Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Array to Hash in Ruby (See related posts)

Inspired by something Ryan Carver was trying to do:

   1  a = [1, 2, 3]
   2  Hash[*a.collect { |v|
   3      [v, v*2]
   4  }.flatten]


It's not as foolproof as his solution, however!

Comments on this post

peter posts on May 21, 2005 at 22:02
testing
therealadam posts on May 21, 2005 at 23:33
Wow, that made my jaw drop. I remixed it as a member of Array and accepting a block to map array keys to Hash values. Just for kicks.


class Array
def to_h(&block)
Hash[*self.collect { |v|
[v, block.call(v)]
}.flatten]
end
end
petef posts on Mar 29, 2007 at 08:35
I renamed therealadam's version to_hash_keys and added a counterpart to_hash_values.

   1  class Array
   2    def to_hash_keys(&block)
   3      Hash[*self.collect { |v|
   4        [v, block.call(v)]
   5      }.flatten]
   6    end
   7  
   8    def to_hash_values(&block)
   9      Hash[*self.collect { |v|
  10        [block.call(v), v]
  11      }.flatten]
  12    end
  13  end


Then I can do this for example:

   1  >> a = ["able", "baker", "charlie"]
   2  >> a.to_hash_values {|v| a.index(v)}
   3  => {0=>"able", 1=>"baker", 2=>"charlie"}
justinwr posts on Jun 02, 2007 at 14:51
This helped me with a small challenge, of comparing two Arrays to each other, then generating a Hash of the results of that compare.

Here's a little messing about in irb...
   1  
   2  >> id_list = (21..31).to_a
   3  => [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
   4  >> products_ids = [24, 29, 30, 32]
   5  => [24, 29, 30, 32]
   6  >> results = Hash[*id_list.collect {|v| [v, products_ids.include?(v)]}.flatten]
   7  => {27=>false, 22=>false, 28=>false, 23=>false, 29=>true, 24=>true, 30=>true, 25=>false, 31=>false, 26=>false, 21=>false}
   8  >> results.each_pair {|k,v| puts "Key: #{k} is true!" if v == true}
   9  Key: 29 is true!
  10  Key: 24 is true!
  11  Key: 30 is true!


basvk posts on Jan 21, 2008 at 17:30
using zip to create a hash from a keys and a values array:

   1  
   2  class << Hash
   3    def create(keys, values)
   4      self[*keys.zip(values).flatten]
   5    end
   6  end


gives:

   1  
   2  >> Hash.create(['a', 'b', 'c'], [1, 2, 3])
   3  => {"a"=>1, "b"=>2, "c"=>3}

bradediger posts on Mar 06, 2008 at 17:13
This should really use flatten_once, not flatten, so that you can do something like x.to_hash_keys{[]} and not have the [] flattened out. flatten_once is something like:

   1  
   2  class Array
   3    def flatten_once
   4      returning([]) {|ary| each{|x| ary.concat x}}
   5    end
   6  end
jkndrkn posts on May 06, 2008 at 15:16
@ bradediger: I had trouble getting your flatten_once routine running. Here is my solution:

class Array
def flatten_once
inject([]) { |v, e| v.concat(e)}
end
end
jkndrkn posts on May 06, 2008 at 15:17
Sorry, newbie here:

   1  
   2  class Array
   3      def flatten_once
   4          inject([]) { |v, e| v.concat(e)}
   5      end 
   6  end

You need to create an account or log in to post comments to this site.


Click here to browse all 5829 code snippets

Related Posts