0

If I have the following array:

foo = ['a', 'b', 'c', 'd', 'e']

Is there a simple way in Ruby to make it into a hash that looks like:

{ 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e' }

I can do this:

Hash[foo.map{|a| [a, a]}]

which works just fine, but I'm curious if there's some other way.

1
  • There are other ways too, but that way is perfectly acceptable and it's concise and would be used most often. Apr 13, 2016 at 3:26

5 Answers 5

5
foo = ['a', 'b', 'c', 'd', 'e']

[foo, foo].transpose.to_h
#=> {"a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d", "e"=>"e"}

foo.zip(foo).to_h
#=> {"a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d", "e"=>"e"}
0
3

Do you need a hash with just those values, or would a hash with a self-default be fine?

For example:

Hash.new { |h,k| h[k] = k }

You can also do this and combine it with itself into pairs:

Hash[foo.zip(foo)]
3
  • Oooh! I like zip()! That's the kind of Ruby magic I was wondering about. :-)
    – Dan Sharp
    Apr 13, 2016 at 3:28
  • 1
    @Dan, it won't be long before you see zip as pretty pedestrian. Apr 13, 2016 at 3:30
  • 1
    Once you have h = Hash.new { |h,k| h[k] = k } => {} you can write h.values_at(*foo) #=> ["a", "b", "c", "d", "e"] which results in h #=> {"a"=>"a", "b"=>"b", "c"=>"c", "d"=>"d", "e"=>"e"}. Apr 13, 2016 at 4:28
2

This is another way of doing it:

foo = ['a', 'b', 'c', 'd', 'e']
foo.inject({}){ |h,k| h[k] = k; h }

As suggested on comments, a better variations of this answer uses the each_with_object, so it gets rid off the trailing ; h:

foo.each_with_object({}) { |e,h| h[e]=e }

The advantage of either is that no intermediate array is constructed.

But the way you mentioned will work just fine:

Hash[foo.map{|a| [a, a]}]
2
  • 5
    I suggest you get rid of the pesky ;h by writing foo.each_with_object({}) { |e,h| h[e]=e }. Either way, it has the advantage that, unlike zip then Hash[] or to_h, no intermediate array is constructed. Apr 13, 2016 at 3:26
  • Agreed. each_with_object is preferable to inject in this case, actually in a lot of cases, because it doesn't require the trailing ;h. Apr 13, 2016 at 3:27
1

Your issue does not make sense. I suspect it is an XY-problem. If you actually don't need to store the key-values in the hash, but just need to return the key back, then:

h = Hash.new{|_, k| k}
h["a"] # => "a"
3
  • @CarySwoveland You were faster. Please undelete your answer.
    – sawa
    Apr 13, 2016 at 3:24
  • It was too close to evedovelli's, so I left a comment instead. Apr 13, 2016 at 3:28
  • It's a weird use-case. I define a bunch of languages and I'm building a default translation table. So if I have languages as: ['en', 'fr', 'de', 'ru'] I want to easily build out the initial translation hash: { 'en' => 'en', 'fr' => 'fr', 'de' => 'de', 'ru' => 'ru' }. That's why I was asking. (note, the algorithm then goes through some other data and adjusts the translation table, but the X => X default makes it easier, in my specific use case)
    – Dan Sharp
    Apr 13, 2016 at 3:28
0

I would try this:

foo  = ['a', 'b', 'c', 'd', 'e']
hash = Hash[foo.zip foo]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.