better way to do this?

J

Joe Laughlin

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
 
D

David A. Black

Hi --

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
 
J

Joe Laughlin

David said:
Hi --

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top