better way to do this?

Discussion in 'Ruby' started by Joe Laughlin, Jul 31, 2004.

  1. Joe Laughlin

    Joe Laughlin Guest

    I have an array that looks like this:
    my_array = [["Jane", "Smith"], ["Bob", "Jones", 1960]]

    I want to copy each element of my_array into a another object (in this case,
    a Gtk::TreeIter).

    I'm currently doing something like this:

    tree_store = Gtk::TreeStore.new(String, String, Integer)
    my_array.each do |person|
    count = 0
    row = tree_store.append(nil)
    person.each do |attribute|
    row[count] = attrib
    count = count + 1
    end
    end

    So, for each attribute that's in my_array, I want to copy it into the
    appropriate row element.

    This works, but how can I do it more cleanly, or more in the ruby spirit?

    Thanks,
    Joe
    Joe Laughlin, Jul 31, 2004
    #1
    1. Advertising

  2. Hi --

    On Sat, 31 Jul 2004, Joe Laughlin wrote:

    > I have an array that looks like this:
    > my_array = [["Jane", "Smith"], ["Bob", "Jones", 1960]]
    >
    > I want to copy each element of my_array into a another object (in this case,
    > a Gtk::TreeIter).
    >
    > I'm currently doing something like this:
    >
    > tree_store = Gtk::TreeStore.new(String, String, Integer)
    > my_array.each do |person|
    > count = 0
    > row = tree_store.append(nil)
    > person.each do |attribute|
    > row[count] = attrib


    (I think you mean 'attribute' there.)

    > count = count + 1
    > end
    > end
    >
    > So, for each attribute that's in my_array, I want to copy it into the
    > appropriate row element.
    >
    > This works, but how can I do it more cleanly, or more in the ruby spirit?


    You could use each_with_index:

    my_array.each do |person|
    row = tree_store.append(nil)
    person.each_with_index {|attr,i| row = attr }
    end


    David

    --
    David A. Black
    David A. Black, Jul 31, 2004
    #2
    1. Advertising

  3. Joe Laughlin

    Joe Laughlin Guest

    David A. Black wrote:
    > Hi --
    >
    > On Sat, 31 Jul 2004, Joe Laughlin wrote:
    >
    >> I have an array that looks like this:
    >> my_array = [["Jane", "Smith"], ["Bob", "Jones", 1960]]
    >>
    >> I want to copy each element of my_array into a another
    >> object (in this case, a Gtk::TreeIter).
    >>
    >> I'm currently doing something like this:
    >>
    >> tree_store = Gtk::TreeStore.new(String, String, Integer)
    >> my_array.each do |person|
    >> count = 0
    >> row = tree_store.append(nil)
    >> person.each do |attribute|
    >> row[count] = attrib

    >
    > (I think you mean 'attribute' there.)


    Doh, I did.

    >
    >> count = count + 1
    >> end
    >> end
    >>
    >> So, for each attribute that's in my_array, I want to
    >> copy it into the appropriate row element.
    >>
    >> This works, but how can I do it more cleanly, or more in
    >> the ruby spirit?

    >
    > You could use each_with_index:
    >
    > my_array.each do |person|
    > row = tree_store.append(nil)
    > person.each_with_index {|attr,i| row = attr }
    > end


    Nice. Thanks for the tip.

    Joe
    Joe Laughlin, Jul 31, 2004
    #3
    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. Earl Teigrob
    Replies:
    3
    Views:
    8,965
    Earl Teigrob
    Jan 6, 2004
  2. Peter Bencsik
    Replies:
    2
    Views:
    813
  3. Paul Rubin
    Replies:
    5
    Views:
    410
    Hendrik van Rooyen
    Aug 6, 2009
  4. Andrew Thompson
    Replies:
    8
    Views:
    136
    Premshree Pillai
    Jun 7, 2005
  5. Replies:
    2
    Views:
    45
    Mark H Harris
    May 13, 2014
Loading...

Share This Page