a = ["item 1", "item 2", "item 3", "item 4"] h = Hash[*a] # => { "item 1" => "item 2", "item 3" => "item 4" }
That’s it. The *
is called the splat operator.
One caveat per @Mike Lewis (in the comments): “Be very careful with this. Ruby expands splats on the stack. If you do this with a large dataset, expect to blow out your stack.”
So, for most general use cases this method is great, but use a different method if you want to do the conversion on lots of data. For example, @Łukasz Niemier (also in the comments) offers this method for large data sets:
h = Hash[a.each_slice(2).to_a]
Related Posts:
- What is the best way to convert an array to a hash in Ruby
- What does %w(array) mean?
- Ruby: undefined method `[]’ for nil:NilClass when trying to get Enumerator on an Array of Hashes
- How to map with index in Ruby?
- How to sum array of numbers in Ruby?
- Ruby array to string conversion
- How to check if a value exists in an array in Ruby
- What is the “right” way to iterate through an array in Ruby?
- Difference between map and collect in Ruby?
- Creating and iterating a 2d array in Ruby
- Array to Hash Ruby
- Sort hash by key, return hash in Ruby
- How can I delete one element from an array by value
- Get underlined text with Markdown
- What is the meaning of *nix?
- Get underlined text with Markdown
- What does the “map” method do in Ruby?
- C# equivalent of C++ map
- How to write a switch statement in Ruby
- Rails 5 ActionController::InvalidAuthenticityToken error
- what does ? ? mean in ruby
- How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite?
- How to use “gets” and “gets.chomp” in Ruby
- Getting a substring in Ruby by x number of chars
- How to check whether a string contains a substring in Ruby
- Ruby: How to install a specific version of a ruby gem?
- ruby operator “=~” [duplicate]
- What does << mean in Ruby?
- What is attr_accessor in Ruby?
- ActionController::UnknownFormat
- What does ||= (or-equals) mean in Ruby?
- How to read lines of a file in Ruby
- “Cannot create generic array of ..” – how to create an Array of Map
? - Ruby equivalent for Python’s “try”?
- What does “+=” (plus equals) mean?
- Rails 4 RoutingError: No Route Matches
- undefined method (NoMethodError) ruby
- Multi-Line Comments in Ruby?
- How to uninstall ruby installed by ruby-install
- PG::ConnectionBad – could not connect to server: Connection refused
- Rails syntax error : unexpected keyword_ensure, expecting end-of-input
- Removing all installed Gems and starting over
- Ruby `send` vs `call` method
- What is the meaning of “do | |” in Ruby?
- RVM is not a function, selecting rubies with ‘rvm use …’ will not work
- How to delete specific characters from a string in Ruby?
- Ruby: kind_of? vs. instance_of? vs. is_a?
- Rails: I installed Ruby, now “bundle install” doesn’t work
- How to do a newline in output
- Ruby: What is the easiest way to remove the first element from an array?
- Ruby String to Date Conversion
- p vs puts in Ruby
- Determining type of an object in ruby
- Fizz Buzz in Ruby for dummies
- How to Uninstall RVM? [duplicate]
- What is the difference between a Process’ pid, ppid, uid, euid, gid and egid?
- Equivalent of “continue” in Ruby
- What does the Ruby method ‘to_sym’ do?
- Ruby 2.0.0p0 IRB warning: “DL is deprecated, please use Fiddle”
- Getting error: dyld: Symbol not found: _clock_gettime
- Uninstall old versions of Ruby gems
- Uploading a file in Rails
- What is java interface equivalent in Ruby?
- Gem installation error: You have to install development tools first
- How to understand strptime vs. strftime
- How to break out from a ruby block?
- ERROR: While executing gem … (Gem::FilePermissionError)
- No such file or directory @ rb_sysopen ruby
- How to add to an existing hash in Ruby
- Ruby ‘require’ error: cannot load such file
- How to convert a string to lower or upper case in Ruby
- What is the canonical way to trim a string in Ruby without creating a new string?
- Difference between $stdout and STDOUT in Ruby
- No implicit conversion of String into Integer (TypeError)?
- Multi-Line Comments in Ruby?
- For-each over an array in JavaScript
- How can I remove a specific item from an array?
- Loop through an array in JavaScript
- Create ArrayList from array
- What is the way of declaring an array in JavaScript?
- “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
- How to convert jsonString to JSONObject in Java
- Java – Best way to print 2D array?
- How to update a value, given a key in a hashmap?
- How do I get an empty array of any size in python?
- Convert array of strings into a string in Java
- How to print elements from array with javascript
- How to loop through an array containing objects and access their properties
- Sort array of objects by object fields
- How to fix “TypeError: len() of unsized object”
- Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
- Check if C++ Array is Null
- Excess elements of scalar initializer for pointer to array of ints
- How to sort an array in Bash
- Uncaught TypeError: Cannot read property ‘toUpperCase’ of undefined
- PHP array printing using a loop
- Difference between char *argv[] and char **argv for the second argument to main()
- C# function to return array
- variably modified array at file scope in C
- Remove empty array elements