{"id":99,"date":"2012-02-08T21:40:43","date_gmt":"2012-02-09T05:40:43","guid":{"rendered":"http:\/\/www.lorrin.org\/blog\/?p=99"},"modified":"2012-03-16T23:26:45","modified_gmt":"2012-03-17T06:26:45","slug":"how-to-treat-a-bare-value-as-an-array-in-ruby","status":"publish","type":"post","link":"https:\/\/www.lorrin.org\/blog\/2012\/02\/08\/how-to-treat-a-bare-value-as-an-array-in-ruby\/","title":{"rendered":"How to treat a bare value as an Array in Ruby"},"content":{"rendered":"<p>Often <code>Array(arg)<\/code> is used for this, but is flawed. Note the last result when applied to a Hash:<\/p>\n<pre class=\"brush:ruby\">&gt; Array(42)\r\n\u00a0=&gt; [42] \r\n&gt; Array([1,2,3])\r\n\u00a0=&gt; [1, 2, 3] \r\n&gt; Array(nil)\r\n\u00a0=&gt; [] \r\n&gt; Array(\"foo\")\r\n\u00a0=&gt; [\"foo\"] \r\n&gt; Array({\"foo\" =&gt; \"bar\", \"biz\" =&gt; \"baz\"})\r\n\u00a0=&gt; [[\"foo\", \"bar\"], [\"biz\", \"baz\"]]<\/pre>\n<p>What went wrong is that Array() calls the (now deprecated) <code>to_a<\/code> on each of its arguments. Hash has a custom <code>to_a<\/code> implementation with different semantics. Instead, do\u00a0 this:<\/p>\n<pre class=\"brush:ruby\">class Array\r\n\u00a0 def self.wrap(args)\r\n\u00a0\u00a0\u00a0 return [] unless args\r\n\u00a0\u00a0\u00a0 args.is_a?(Array) ? args : [args]\r\n\u00a0 end\r\nend<\/pre>\n<p>That yields the expected results, even for Hashes:<\/p>\n<pre class=\"brush:ruby\">&gt; Array.wrap(42)\r\n\u00a0=&gt; [42] \r\n&gt; Array.wrap([1,2,3])\r\n\u00a0=&gt; [1, 2, 3] \r\n&gt; Array.wrap(nil)\r\n\u00a0=&gt; [] \r\n&gt; Array.wrap(\"foo\")\r\n\u00a0=&gt; [\"foo\"] \r\n&gt; Array.wrap({\"foo\" =&gt; \"bar\", \"biz\" =&gt; \"baz\"})\r\n\u00a0=&gt; [{\"foo\"=&gt;\"bar\", \"biz\"=&gt;\"baz\"}]<\/pre>\n<p>Use of <code>is_a?<\/code> is deliberate; duck-typing in this situation (<code>[:[], :each].all? { |m| args.respond_to? m }<\/code>) yields unexpected surprises since e.g. String is Enumerable and would not get wrapped.<\/p>\n<p>For further discussion see Ruby-forum thread &#8220;<a href=\"http:\/\/www.ruby-forum.com\/topic\/145515\">shortcut for x = [x] unless x.is_a?(Array)<\/a>&#8221; and StackOverflow &#8220;<a href=\"http:\/\/stackoverflow.com\/questions\/385912\/ruby-object-to-a-replacement\">Ruby: Object.to_a replacement<\/a>&#8220;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often Array(arg) is used for this, but is flawed. Note the last result when applied to a Hash: &gt; Array(42) \u00a0=&gt; [42] &gt; Array([1,2,3]) \u00a0=&gt; [1, 2, 3] &gt; Array(nil) \u00a0=&gt; [] &gt; Array(&#8220;foo&#8221;) \u00a0=&gt; [&#8220;foo&#8221;] &gt; Array({&#8220;foo&#8221; =&gt; &#8220;bar&#8221;, &#8220;biz&#8221; =&gt; &#8220;baz&#8221;}) \u00a0=&gt; [[&#8220;foo&#8221;, &#8220;bar&#8221;], [&#8220;biz&#8221;, &#8220;baz&#8221;]] What went wrong is that Array() calls <a href='https:\/\/www.lorrin.org\/blog\/2012\/02\/08\/how-to-treat-a-bare-value-as-an-array-in-ruby\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[48],"tags":[31],"_links":{"self":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":4,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions\/154"}],"wp:attachment":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}