{"id":7017,"date":"2014-05-17T00:20:54","date_gmt":"2014-05-17T00:20:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/key-value-ruby-method-declaration-closed-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:20:54","modified_gmt":"2014-05-17T00:20:54","slug":"key-value-ruby-method-declaration-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/key-value-ruby-method-declaration-closed-collection-of-common-programming-errors\/","title":{"rendered":"Key-Value Ruby method declaration [closed]-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>And how do I access the Key and value for this method? Is there a good resource to answer these types of questions? Ruby syntax is a little strange to me.<\/p>\n<\/blockquote>\n<p>Well yes, the Ruby syntax is strange when you don&#8217;t acknowledge it, but fun and powerful once mastered. Ruby has a very concise and possibly confusing syntax for hashes and arrays.<\/p>\n<p>For example an array would be normally generated by the following syntax:<\/p>\n<pre><code>x = [1, 2, 3]\n<\/code><\/pre>\n<p>but you can shorten it up to:<\/p>\n<pre><code>x = 1, 2, 3\n<\/code><\/pre>\n<p>In your case while generally that method can be called as:<\/p>\n<pre><code>a.method( { \"hello\" =&gt; \"bye\" } )\n<\/code><\/pre>\n<p>it can also be called as you defined in your question&#8217;s code. The fact is that in that case there&#8217;s not ambiguity, as two <code>String<\/code>s paired with a <code>=&gt;<\/code> can <em>only<\/em> mean that you are producing an <code>Hash<\/code>.<\/p>\n<p>This means that to reproduce it you can just create a method that accepts an hash and deals with the values:<\/p>\n<pre><code>def method( hash )\n    # hash.keys.first is how you can access the key ( \"hello\" )\n    # hash.first is how you can access the value ( \"bye\" )\nend\n<\/code><\/pre>\n<p>Now, you have to keep in mind that with the method above, anyone using it might also pass multiple values (more than one) or even a non hash parameter, that you either choose to ignore or <em>deal<\/em> with, like in the following example:<\/p>\n<pre><code>def method( hash )\n    raise ArgumentError \"...\" unless hash.is_a? Enumerable\n    raise ArgumentError \"...\" if hash.size &gt; 1\n    # ...\nend\n<\/code><\/pre>\n<p>In the first line you raise an exception if the hash cannot be enumerated (like <code>Hash<\/code>es do). In the second one you check for the number of key-values in the hash and raise an exception if it is more than one.<\/p>\n<p>The final thing to notice is that you cannot take more than one argument out for that method and expect it to work with that syntax:<\/p>\n<pre><code>def method( hash, a ); end\nmethod( \"hello\" =&gt; \"bye\", 1 )\n# =&gt; SyntaxError: syntax error, unexpected ')'\n<\/code><\/pre>\n<p>even though, it can be <em>resolved<\/em> by using:<\/p>\n<pre><code>method( { \"hello\" =&gt; \"bye\" }, 1 )\n<\/code><\/pre>\n<p>Have fun.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>And how do I access the Key and value for this method? Is there a good resource to answer these types of questions? Ruby syntax is a little strange to me. Well yes, the Ruby syntax is strange when you don&#8217;t acknowledge it, but fun and powerful once mastered. Ruby has a very concise and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7017","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7017"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7017\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}