Dynamically create hash

Discussion in 'Ruby' started by Matthias Wiemann, Oct 11, 2007.

  1. Hi,
    i would like to dynamically create hashes, so that I can create them by
    looping over the names.

    Something like:

    for i in 1..10
    "hash#{i}" = Hash.new
    "hash#{i}" = [:key => "val"]
    end

    So the hashes created would go by the names hash1, hash2, hash3...

    Does anyone know how to do that?

    thanks
    mat
    --
    Posted via http://www.ruby-forum.com/.
    Matthias Wiemann, Oct 11, 2007
    #1
    1. Advertising

  2. Matthias Wiemann wrote:
    > Hi,
    > i would like to dynamically create hashes, so that I can create them by
    > looping over the names.


    You can do that using eval (or using instance variables), but that's evil.
    It would probably make more sense to use an array or hash of hashes.

    > Something like:
    >
    > for i in 1..10
    > "hash#{i}" = Hash.new
    > "hash#{i}" = [:key => "val"]
    > end
    >
    > So the hashes created would go by the names hash1, hash2, hash3...


    hashes=Array.new(10) do |i|
    {:key => "val"}
    end

    This gives you hashes[0] to hashes[9].


    HTH,
    Sebastian
    --
    NP: Black Sabbath - Heaven and Hell
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Oct 11, 2007
    #2
    1. Advertising

  3. I've no idea why you want this, but I think you want to have an array of
    hashes to iterate through.

    like this:

    hashes = []
    for i in 0..9
    hashes = {"key" => "val"}
    end

    so hashes contains
    [{"key"=>"val"}, {"key"=>"val"}, {"key"=>"val"}, {"key"=>"val"},
    {"key"=>"val"}, {"key"=>"val"}, {"key"=>"val"}, {"key"=>"val"},
    {"key"=>"val"}, {"key"=>"val"}]


    Matthias Wiemann wrote:
    > Hi,
    > i would like to dynamically create hashes, so that I can create them by
    > looping over the names.
    >
    > Something like:
    >
    > for i in 1..10
    > "hash#{i}" = Hash.new
    > "hash#{i}" = [:key => "val"]
    > end
    >
    > So the hashes created would go by the names hash1, hash2, hash3...
    >
    > Does anyone know how to do that?
    >
    > thanks
    > mat


    --
    Posted via http://www.ruby-forum.com/.
    Joachim Glauche, Oct 11, 2007
    #3
  4. Matthias Wiemann, Oct 11, 2007
    #4
  5. Matthias Wiemann

    Brian Adkins Guest

    On Oct 11, 6:57 am, Matthias Wiemann <> wrote:
    > Hi,
    > i would like to dynamically create hashes, so that I can create them by
    > looping over the names.
    >
    > Something like:
    >
    > for i in 1..10
    > "hash#{i}" = Hash.new
    > "hash#{i}" = [:key => "val"]
    > end
    >
    > So the hashes created would go by the names hash1, hash2, hash3...
    >
    > Does anyone know how to do that?


    As other posters have mentioned, it's quite possible you don't want to
    do that, but instead want an Array or Hash of Hashes to allow you to
    more easily access the individual Hashes.

    However, that's not as fun as exploring the possibilities :) You could
    use Object#instance_variable_set, but then you'd have @hash1,
    @hash2, ... instead of hash1, hash2, ...

    Using Kernel#eval e.g. eval "hash1={:key=>value}; ... " won't work
    because as of Ruby 1.8, you're required to define local variables in
    the outer scope prior to executing eval which would defeat the
    purpose.

    However, I noticed Kernel#eval accepts a binding parameter, so the
    following may do what you thought you wanted to do (but probably don't
    actually want to do):

    eval (1..10).inject("") {|m,o| m << "hash#{o}={:key =>
    'value#{o}'};" }, binding

    Brian Adkins

    --
    http://www.lojic.com
    http://lojic.com/blog
    Brian Adkins, Oct 11, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    477
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    254
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    212
    Nobuyoshi Nakada
    Jun 13, 2007
  4. aidy
    Replies:
    4
    Views:
    111
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    580
    David A. Black
    Jul 2, 2008
Loading...

Share This Page