tags:

views:

215

answers:

3

hi

I have this code:

Shoes.app do

  data = [1,2,3,4] # could be also more

  data.each { |i|
    edit_line ("foo"){ puts i}
  }

end

when the value (foo) change in the edit_line field, I see in the terminal 1,2,3 or 4. But I need the value (.txt) from each edit_line field. How can I reference to it? The problem is that data is dynamic and can have n entries.

In php there is something like $$var is there something in ruby? maybe this could help.

A: 

a friend point me to this:

  
data.each_index { |i| data[i] = edit_line() { data[i].text } }
A: 

Since you need both the index and data stored there, you want #each_with_index. I've never used Shoes so I can't help with that part, but #each_with_index works like so:

data = ["cat", "bat", "rat"]
data.each_with_index {|i, d| print "#{i} => #{d} "}

This will print: 0 => cat 1 => bat 2 => rat

David Seiler
+1  A: 

If you just want to see the data when an edit_line changes, you can make use of the change method and the text method:

Shoes.app do

  data = [1,2,3,4] # could be also more

  data.each do |i|
    edit_line('foo').change do |e| { puts "#Edit Line #{i} Changed: #{e.text}" }
  end
end

Since the default block to the edit_line method does the same thing, you can simplify this to:

Shoes.app do

  data = [1,2,3,4] # could be also more

  data.each do |i|
    edit_line 'foo' do |e| { puts "#Edit Line #{i} Changed: #{e.text}" }
  end
end

Also, note that using do/end instead of {} for multi-line blocks is the preferred style.

Pesto