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 does << mean in Ruby?
- Array to Hash Ruby
- What is the best way to convert an array to a hash in Ruby
- How to split a string into an array in Bash?
- Converting array to list in Java
- What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
- Java Error “Exception in thread “main” java.util.InputMismatchException” On an Array program
- How to print elements in a vector c++
- TypeScript Objects as Dictionary types as in C#
- Data type not understood while creating a NumPy array
- “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
- Are vectors passed to functions by value or by reference in C++
- How do I declare a 2d array in C++ using new?
- C# equivalent of C++ map
- “error: assignment to expression with array type error” when I assign a struct field (C)
- Does JavaScript have a method like “range()” to generate a range within the supplied bounds?
- Getting “conflicting types for function” in C, why?
- Javascript Uncaught TypeError: Cannot read property ‘0’ of undefined
- Does JavaScript have a method like “range()” to generate a range within the supplied bounds?
- Form submission: PHP S_SESSION statements within a foreach loop
- How to split a string into an array in Bash?
- Working with a List of Lists in Java
- Array Size (Length) in C#
- Getting “conflicting types for function” in C, why?
- How to convert jsonString to JSONObject in Java
- Array of arrays (Python/NumPy)
- what does “>>>” mean in java?
- How to convert List
to int[] in Java? - Calculating Standard Deviation & Variance in C++
- What does %w(array) mean?
- Javascript Uncaught TypeError: Cannot read property ‘0’ of undefined
- How to convert int[] into List
in Java? - What is a list in Bash?
- What does ** do in C language?
- Correct way of looping through C++ arrays
- When will the worst case of Merge Sort occur?
- In Java, how to append a string more efficiently?
- How do I create an array of strings in C?
- ow to create a histogram in java
- what does .space do in mips?
- How do I remove an array item in TypeScript?
- How to create an array of strings in C?
- Initialization from incompatible pointer type warning when assigning to a pointer
- “Cannot create generic array of ..” – how to create an Array of Map
? - Ruby: undefined method `[]’ for nil:NilClass when trying to get Enumerator on an Array of Hashes
- Bubble sort algorithm in MIPS
- How to map with index in Ruby?
- Error: unsupported use of matrix or array for column indexing
- Using multiple variables in a for loop in Python
- Weighted random selection from array
- Badly placed ()’s error with the following shell script
- How to sum array of numbers in Ruby?
- type any? has no subscript members
- Ruby array to string conversion
- How to check if a value exists in an array in Ruby
- Multidimensional Vectors in C++
- Java ArrayList for integers
- java.lang.ArrayIndexOutOfBoundsException: 4 Error
- IndexError: index 10 is out of bounds for axis 0 with size 10
- How to sort an array in Bash
- How can I find the number of elements in an array?
- Array type char[] is not assignable
- Two-dimensional array in Swift
- Parameter name omitted error?
- What is the “right” way to iterate through an array in Ruby?
- C char array initialization
- How to iterate through table in Lua?
- How to sort in-place using the merge sort algorithm?
- Difference between map and collect in Ruby?
- VBA array sort function?
- How to declare and use 1D and 2D byte arrays in Verilog?
- Difference between char *argv[] and char **argv for the second argument to main()
- Convert ArrayList
to java.util.List [closed] - Dynamic vs static array in c
- Creating and iterating a 2d array in Ruby
- Print array elements on separate lines in Bash?
- Java says this method has a constructor name
- Create an empty object in JavaScript with {} or new Object()?
- Sort hash by key, return hash in Ruby
- GCC: Array type has incomplete element type
- What is the best way to delete a value from an array in Perl?
- How can I delete one element from an array by value
- Checking if a key exists in a JavaScript object?
- variably modified array at file scope in C
- Numpy matrix to array
- Using the Pythagorean theorem with Java
- What is the way of declaring an array in JavaScript?
- Getting a substring in Ruby by x number of chars
- How to loop through an array containing objects and access their properties
- Python: Can a function return an array and a variable?
- How to make an array of arrays in Java
- How to add a string to a string[] array? There’s no .Add function
- Ruby `send` vs `call` method
- Checking to see if array is full
- How to allocate array of pointers for strings by malloc in C?
- How to print the full NumPy array, without truncation?
- C++ 2d char array to string
- Fastest way to implode an associative array with keys
- C# function to return array
- No such file or directory @ rb_sysopen ruby