Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - mapping an array to hashmap

Tags:

hashmap

ruby

I have an array, and a function that returns a value given a value. Ultimately I want to create a hashmap that has the values of the array as key value, and the result of f(key_value) as the value. Is there a clean, simple way, like similar to each/map of Array, of doing this using block?

So something that is equivalent to

hsh = {}
[1,2,3,4].each do |x|
  hsh[x] = f(x)
end

but looks more similar to this, in that it's simple and one line?

results = array.map { | x | f(x) }
like image 782
Ji Mun Avatar asked Oct 22 '12 18:10

Ji Mun


People also ask

How do you turn an array into a hash in Ruby?

The to_h method is defined in the array class. It works to convert an array to a hash in the form of key-value pairs. The method converts each nested array into key-value pairs. The method also accepts a block.

How do I map an array in Ruby?

The way the map method works in Ruby is, it takes an enumerable object, (i.e. the object you call it on), and a block. Then, for each of the elements in the enumerable, it executes the block, passing it the current element as an argument. The result of evaluating the block is then used to construct the resulting array.

How do you turn an array into a map?

To convert an array of objects to a Map , call the map() method on the array and on each iteration return an array containing the key and value. Then pass the array of key-value pairs to the Map() constructor to create the Map object.

Can you use map with hash Ruby?

Map is a Ruby method that you can use with Arrays, Hashes & Ranges. The main use for map is to TRANSFORM data. For example: Given an array of strings, you could go over every string & make every character UPPERCASE.


6 Answers

Note that since Ruby 2.1.0 you can also use Array#to_h, like this:

[1,2,3,4].map{ |x| [x, f(x)] }.to_h
like image 118
Knotty66 Avatar answered Oct 02 '22 13:10

Knotty66


Ruby 2.6.0 enables passing a block to the to_h-method. This enables an even shorter syntax for creating a hash from an array:

[1, 2, 3, 4].to_h { |x| [x, f(x)] }
like image 26
Timitry Avatar answered Oct 02 '22 12:10

Timitry


You could also define the function as the hash's default value:

hash = Hash.new {|hash, key| hash[key] = f(key) }

Then when you lookup a value, the hash will calculate and store it on the fly.

hash[10]
hash.inspect #=> { 10 => whatever_the_result_is }
like image 40
Zach Kemp Avatar answered Oct 02 '22 11:10

Zach Kemp


You need each_with_object.

def f x
  x * 2
end

t = [1, 2, 3, 4].each_with_object({}) do |x, memo|
  memo[x] = f(x)
end

t # => {1=>2, 2=>4, 3=>6, 4=>8}

Another one:

t2 = [1, 2, 3, 4].map{|x| [x, f(x)]}
Hash[t2] # => {1=>2, 2=>4, 3=>6, 4=>8}
like image 35
Sergio Tulentsev Avatar answered Oct 02 '22 13:10

Sergio Tulentsev


Check out the Hash::[] method.

Hash[ [1,2,3,4].collect { |x| [x, f(x)] } ]
like image 37
Matt Huggins Avatar answered Oct 02 '22 12:10

Matt Huggins


Using Facets' mash (method to convert enumerable to hashes):

[1, 2, 3, 4].mash { |x| [x, f(x)] }

From Ruby 2.1:

[1, 2, 3, 4].map { |x| [x, f(x)] }.to_h
like image 25
tokland Avatar answered Oct 02 '22 12:10

tokland