{"id":13306,"date":"2025-08-21T06:47:57","date_gmt":"2025-08-21T06:47:57","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13306"},"modified":"2025-08-21T06:47:57","modified_gmt":"2025-08-21T06:47:57","slug":"keyword-argument-vs-hash","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/keyword-argument-vs-hash","title":{"rendered":"Method Arguments: Keyword Argument vs Hash"},"content":{"rendered":"<h3>Problem Statement<\/h3>\n<p>In Rails, you might see:<\/p>\n<pre class=\"lang:adddarkplain\">validates :name, presence: true<\/pre>\n<p>Is <strong>presence: true<\/strong> a keyword argument or a hash?<\/p>\n<h2>Answer<\/h2>\n<p>Ruby allows both keyword arguments and regular arguments, including hashes. Before Ruby 2.7, keyword arguments were just hashes passed as the last argument. From Ruby 2.7 and especially Ruby 3.0 onward, keyword arguments are now strictly separated from positional hashes.<\/p>\n<p>So depending on the Ruby version, this line:<\/p>\n<p>Could be interpreted as:<\/p>\n<ul>\n<li><strong>Ruby < 2.7: presence: true<\/strong> is just a Hash.<\/li>\n<li><strong>Ruby \u2265 2.7: presence: true<\/strong> is a keyword argument unless the method explicitly accepts a trailing Hash.<\/li>\n<\/ul>\n<p>In Rails, most DSL methods like <strong>validates <\/strong>are designed to work with either form.<\/p>\n<h2>Rails Example<\/h2>\n<p>Rails defines validates like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/08\/21064535\/unnamed-5.png\" alt=\"Rails\" width=\"453\" height=\"110\" class=\"alignnone size-full wp-image-13308\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/08\/21064535\/unnamed-5.png 453w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/08\/21064535\/unnamed-5-300x73.png 300w\" sizes=\"(max-width: 453px) 100vw, 453px\" \/><\/p>\n<p>Here, the <strong>presence: true<\/strong> part is extracted as a hash from the arguments array.<\/p>\n<h2>General Rule<\/h2>\n<ul>\n<li><strong>*args + extract_options! \u2192<\/strong> handles trailing hashes<\/li>\n<li><strong>**kwargs <\/strong>\u2192 handles keyword arguments explicitly<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In most Rails DSLs, options like<strong> presence: true <\/strong>are passed as trailing hashes. No need to worry unless you&#8217;re designing custom APIs or methods.<\/p>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/using-rails-active-storage\" target=\"_blank\">Ruby on Rails Active Storage<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement In Rails, you might see: validates :name, presence: true Is presence: true a keyword argument or a hash? Answer Ruby allows both keyword arguments and regular arguments, including hashes. Before Ruby 2.7, keyword arguments were just hashes passed as the last argument. From Ruby 2.7 and especially Ruby 3.0 onward, keyword arguments are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-13306","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13306"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13306"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13306\/revisions"}],"predecessor-version":[{"id":13309,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13306\/revisions\/13309"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13307"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}