DISQUS

Virtuous Code: Array-ifying Values

  • Bradly Feeley · 2 months ago
    You can use [*items] as well to do the same thing as Array(). I don't know if there are any reasons to use one over the other, but it's an option.
  • avdi · 2 months ago
    Indeed. I personally feel that Array() is more intent-revealing, but [*] is certainly an alternative.
  • phiggy · 1 month ago
    They differ in the nil case:

    >> RUBY_DESCRIPTION # => "ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]"
    >> [*nil] # => [nil]
    >> Array(nil) # => []
  • Pablo Villalba · 1 month ago
    I updated a blog post I did on the subject with links to this post, thanks!

    http://blog.teambox.com/mrproper-cleaner-blocks...
  • Evgeniy Dolzhenko · 1 month ago
    One thing I don't like about the behavior with Hash transformed to Array is that you can't use it to "normalize" argument to an array (for example to get an API which allows passing both array of hashes and singe hash) so you have to resort to something like `[arg].flatten`.