Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Apr 17 ’12

Create a hash from an array with Hash[*arr]

The Hash::[] class method can be used in addition to the splat operator to create a hash from an array.

arr = [:a, 1, :b, 2]
Hash[*arr]
# => {:a => 1, :b => 2}

7 notes 0 comments

  1. raul-b-blog reblogged this from rubyquicktips
  2. haru012 reblogged this from rubyquicktips
  3. kikito submitted this to rubyquicktips

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus