double assignation in a hash

Discussion in 'Ruby' started by nico Itkin, Jun 5, 2008.

  1. nico Itkin

    nico Itkin Guest

    hi !

    I'm new to ruby, i'd like to know if it is possible to make the
    following kind of assignation in a hash, like i used to do in php :

    a={}
    a[:b][:c]="value"

    the goal for me is to create a hash by iterating on keys, like

    hash={}
    [a,b,c].each do |k1|
    [d,e,f].each do |k2|
    hash[k1][k2] = afunction(k1,k2)
    end
    end

    this last code lead to a nil.[] error

    Could you help me ? .
    --
    Posted via http://www.ruby-forum.com/.
     
    nico Itkin, Jun 5, 2008
    #1
    1. Advertising

  2. On Jun 4, 2008, at 7:23 PM, nico Itkin wrote:

    > hi !
    >
    > I'm new to ruby, i'd like to know if it is possible to make the
    > following kind of assignation in a hash, like i used to do in php :
    >
    > a={}
    > a[:b][:c]="value"
    >
    > the goal for me is to create a hash by iterating on keys, like
    >
    > hash={}
    > [a,b,c].each do |k1|
    > [d,e,f].each do |k2|
    > hash[k1][k2] = afunction(k1,k2)
    > end
    > end
    >
    > this last code lead to a nil.[] error
    >
    > Could you help me ? .



    Your problem is that a[:b] is not a hash. You can get this effect by
    defining a default for the hash 'a' like so:

    irb> a = Hash.new {|h,k| h[k] = {} }
    => {}
    irb> a[:b][:c] = 'value'
    => "value"
    irb> a
    => {:b=>{:c=>"value"}}

    The block supplied to Hash.new is called with the hash and the key
    when the key doesn't exist. So the effect is as if you had:

    a = {}
    a[:b] = {}
    a[:b][:c] = 'value'

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Jun 5, 2008
    #2
    1. Advertising

  3. On Thu, Jun 05, 2008 at 08:23:53AM +0900, nico Itkin wrote:
    > hi !
    >
    > I'm new to ruby, i'd like to know if it is possible to make the
    > following kind of assignation in a hash, like i used to do in php :
    >
    > a={}
    > a[:b][:c]="value"
    >
    > the goal for me is to create a hash by iterating on keys, like
    >
    > hash={}
    > [a,b,c].each do |k1|
    > [d,e,f].each do |k2|
    > hash[k1][k2] = afunction(k1,k2)
    > end
    > end
    >
    > this last code lead to a nil.[] error
    >
    > Could you help me ? .


    You may find this of interest:

    http://redcorundum.blogspot.com/2007/05/just-nifty-meta-meta-tidbit.html

    --Greg
     
    Gregory Seidman, Jun 5, 2008
    #3
  4. On Thu, Jun 5, 2008 at 1:23 AM, nico Itkin <> wrote:
    > hi !
    >
    > I'm new to ruby, i'd like to know if it is possible to make the
    > following kind of assignation in a hash, like i used to do in php :
    >
    > a={}
    > a[:b][:c]="value"
    >


    For arbitrary hash nesting, try this:

    irb(main):023:0> a = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
    irb(main):024:0> a[1][2][3][4] = "hello"
    irb(main):025:0> a
    => {1=>{2=>{3=>{4=>"hello"}}}}
    irb(main):026:0> a[1][2][3][4]
    => "hello"


    Hope this helps,

    Jesus.
     
    Jesús Gabriel y Galán, Jun 5, 2008
    #4
  5. nico Itkin

    Dave Bass Guest

    Jesús Gabriel y Galán wrote:
    > For arbitrary hash nesting, try this:
    >
    > irb(main):023:0> a = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}
    > irb(main):024:0> a[1][2][3][4] = "hello"
    > irb(main):025:0> a
    > => {1=>{2=>{3=>{4=>"hello"}}}}
    > irb(main):026:0> a[1][2][3][4]
    > => "hello"


    Or this (longer but clearer IMO):

    a = {}
    a[1] = {}
    a[1][2] = {}
    a[1][2][3] = {}
    a[1][2][3][4] = "hello"

    print a[1][2][3][4] # "hello"
    --
    Posted via http://www.ruby-forum.com/.
     
    Dave Bass, Jun 6, 2008
    #5
  6. nico Itkin

    nico Itkin Guest

    Thanks evebody for yours answers, helps a lot :) !

    Jesús : looks fantastic, but could you give me fex explanation how it
    works?
    > a = Hash.new {|h,k| h[k] = Hash.new(&h.default_proc)}


    is this a way to override the method new for the object a?

    You're doing so by passing the following block but then i'm lost, do you
    have any links to share which detail that ?

    Nico (so much to learn...)
    --
    Posted via http://www.ruby-forum.com/.
     
    nico Itkin, Jun 8, 2008
    #6
  7. On Sun, Jun 8, 2008 at 10:25 PM, nico Itkin <> wrote=
    :
    > Thanks evebody for yours answers, helps a lot :) !
    >
    > Jes=FAs : looks fantastic, but could you give me fex explanation how it
    > works?
    >> a =3D Hash.new {|h,k| h[k] =3D Hash.new(&h.default_proc)}

    >
    > is this a way to override the method new for the object a?
    >
    > You're doing so by passing the following block but then i'm lost, do you
    > have any links to share which detail that ?


    Hash.new receives a block, which is executed whenever you try to access
    a non-existing key. In the block you can assign a value to the key in the h=
    ash.
    The value that we assign above is a hash. If we did this:

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

    Then we would achieve a two-level hash, but the second level hash,
    wouldn't be able to assign a next level hash to a non-existing key, because
    a block wasn't provided.

    Hash has a default_proc variable that contains the block (in proc form)
    that was passed to the constructor, so if we assign this proc as the block
    for the constructor, we get a hash that can assign a hash to a non-existing=
    key,
    in a recursive way, cause each hash will pass its default proc to the hash
    created in the constructor block.

    So many words that I don't know if I made myself clear...

    Jesus.
     
    Jesús Gabriel y Galán, Jun 9, 2008
    #7
    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. Sydex
    Replies:
    12
    Views:
    6,658
    Victor Bazarov
    Feb 17, 2005
  2. Denis Pithon
    Replies:
    10
    Views:
    680
  3. =?ISO-8859-1?Q?Xavier_D=E9coret?=

    Controlling assignation

    =?ISO-8859-1?Q?Xavier_D=E9coret?=, Jun 13, 2005, in forum: Python
    Replies:
    14
    Views:
    516
    Bengt Richter
    Jun 26, 2005
  4. Julien Lochen

    vhdl hexa assignation

    Julien Lochen, Dec 5, 2008, in forum: VHDL
    Replies:
    0
    Views:
    1,062
    Julien Lochen
    Dec 5, 2008
  5. rp
    Replies:
    1
    Views:
    595
    red floyd
    Nov 10, 2011
Loading...

Share This Page