Topic: Array to hash

I have an array, i need to make an hash using the array content as values. I need to create keys and map them to values in the array.
When i create keys, i should create names of keys as key0, key1, keyn...where n is the length of the array.

what is the best way of doing this? I have found this on web, but i fell we can do more succint than that.

class Array  def to_h(&block)    Hash[*self.collect { |v|      [v, block.call(v)]    }.flatten]  endend
class Array
  def to_hash_keys(&block)
    Hash[*self.collect { |v|
      [v, block.call(v)]
    }.flatten]
  end

  def to_hash_values(&block)
    Hash[*self.collect { |v|
      [block.call(v), v]
    }.flatten]
  end
end

usage:

>> a = ["able", "baker", "charlie"]
>> a.to_hash_values {|v| a.index(v)}
=> {0=>"able", 1=>"baker", 2=>"charlie"}

Re: Array to hash

That's an interesting requirement. Why do you need to convert it to a hash?

But, to answer your question, you could do this:

h = {}
a = ["able", "baker", "charlie"]
a.each_with_index { |e, i| h[i] = e }
h # => {0=>"able", 1=>"baker", 2=>"charlie"}

Railscasts - Free Ruby on Rails Screencasts

Re: Array to hash

Thanks for your quick reply. well, I will have to form an hash to pass to a method which assumes an hash as its arugument.

1) in your example, how can i do key0, key1, key2..keyn? that is prepending a string to index automatically in the block?

Re: Array to hash

If you have control over the method you are passing it to, consider making the method accept an array as well.

In order to put "key" in the key, try this.

a.each_with_index { |e, i| h["key#{i}"] = e }

Railscasts - Free Ruby on Rails Screencasts

Re: Array to hash

Thanks that might work.

Re: Array to hash

how can i do that for more than one array in the same block?

Re: Array to hash

Not sure what you mean, can you go into more detail?

Railscasts - Free Ruby on Rails Screencasts

Re: Array to hash

Can i do the following in a better way?
params[:receiveremail].each_with_index { |e, i| groups["l_email#{i}"] = e }     
    params[:amount].each_with_index { |e, i| groups["l_amt#{i}"] = e }

Re: Array to hash

You can create a method on array to do this:

class Array
  def to_prefixed_hash(prefix = "key") # may want to rename this
    h = {}
    each_with_index { |e, i| h["#{prefix}#{i}"] = e }
    h
  end
end

Then just call that method and pass the prefix you want.

Railscasts - Free Ruby on Rails Screencasts

Re: Array to hash

so the prefix is the array?

Re: Array to hash

nope, you call this method on the array.

params[:receiveremail].to_prefixed_hash('l_email')

Railscasts - Free Ruby on Rails Screencasts

Re: Array to hash

i see i get it. One of the benefits of metaprogramming of Ruby. Adding a new method to an already defined class. This begs another question. What is the right place to define this new method for the Array class?

in a separate module?

Re: Array to hash

You can just put that code in a file in the lib directory.

Railscasts - Free Ruby on Rails Screencasts

Re: Array to hash

when you said put it in lib folder did you mean Rails or Ruby's?

Ruby has
lib\ruby\1.8\*

Rails has

lib\tasks structure

Re: Array to hash

The "lib" directory for your specific rails project (not the generic Ruby one). You may need to "require" this file in environment.rb if it doesn't get loaded automatially.

Railscasts - Free Ruby on Rails Screencasts