librelist archives

« back to archive

Buttons array

Buttons array

From:
Elias Crespin
Date:
2010-12-24 @ 17:27
Hello,

I need to create an array of tens of buttons. The problem I need your help
for is that I don't find a way to identify which is the index of clicked
button within the click block. Thanks

Something like

for i in 0..219
@b[i] = button "Pm"+i.to_s {
alert "button clicked but how to know which index ?"
}
@b[i].move ((i/20)*60 ), ((i % 20)*20 + 1)
end

Re: [shoes] Buttons array

From:
ashbb
Date:
2010-12-24 @ 18:03
Hi Elias,

Revised your code a little bit. ;-)
Try this one.

Shoes.app do
  @b = []
  for i in 0..219
    @b[i] = button( "Pm"+i.to_s ){
      alert "button clicked but how to know which index ?"
    }
    @b[i].move ((i/20)*60 ), ((i % 20)*20 + 1)
  end
end

Hope this helps,
ashbb

Re: [shoes] Buttons array

From:
Kevin Ball
Date:
2010-12-24 @ 18:15
I think the core question is how to get the value in i to stick around.  The
solution is to make i be a local variable that gets bound within the closure
of each block, rather than one that sticks around to change.

Instead of using

for i in 0..219

try

(0..219).each do |i|

The difference is in the first one you have a single variable getting
assigned multiple values, and every block creates a closure referencing that
variable, so at the end of the loop they are all pointing at a variable that
is valued 219.

In the second on, we create a new local variable i in each iteration through
the loop, so each button creates a closure referencing its own variable.

-Kevin

On Fri, Dec 24, 2010 at 10:03 AM, ashbb <ashbbb@gmail.com> wrote:

> Hi Elias,
>
> Revised your code a little bit. ;-)
> Try this one.
>
> Shoes.app do
>   @b = []
>
>   for i in 0..219
>     @b[i] = button( "Pm"+i.to_s ){
>       alert "button clicked but how to know which index ?"
>     }
>     @b[i].move ((i/20)*60 ), ((i % 20)*20 + 1)
>   end
> end
>
> Hope this helps,
> ashbb
>
>
>

Re: [shoes] Buttons array

From:
Kevin Ball
Date:
2010-12-24 @ 18:28
I realized this may not have been one hundred percent clear...

once you change the way you're assigning that variable, you should be able
to use it within your button click block and have it retain its number.
 Some sample code (just showing the principle, not using shoes):

@b = []
@c = []

(0..210).each do |i|-
  @b[i] = lambda {i}
end

for j in 0..210
  @c[j] = lambda {j}
end

puts "@b[2]:  #{@b[2].call}"
puts "@c[2]:  #{@c[2].call}"


This results in

@b[2]:  2
@c[2]:  210


On Fri, Dec 24, 2010 at 10:15 AM, Kevin Ball <kmball11@gmail.com> wrote:

> I think the core question is how to get the value in i to stick around.
>  The solution is to make i be a local variable that gets bound within the
> closure of each block, rather than one that sticks around to change.
>
> Instead of using
>
> for i in 0..219
>
> try
>
> (0..219).each do |i|
>
> The difference is in the first one you have a single variable getting
> assigned multiple values, and every block creates a closure referencing that
> variable, so at the end of the loop they are all pointing at a variable that
> is valued 219.
>
> In the second on, we create a new local variable i in each iteration
> through the loop, so each button creates a closure referencing its own
> variable.
>
> -Kevin
>
>
> On Fri, Dec 24, 2010 at 10:03 AM, ashbb <ashbbb@gmail.com> wrote:
>
>> Hi Elias,
>>
>> Revised your code a little bit. ;-)
>> Try this one.
>>
>> Shoes.app do
>>   @b = []
>>
>>   for i in 0..219
>>     @b[i] = button( "Pm"+i.to_s ){
>>       alert "button clicked but how to know which index ?"
>>     }
>>     @b[i].move ((i/20)*60 ), ((i % 20)*20 + 1)
>>   end
>> end
>>
>> Hope this helps,
>> ashbb
>>
>>
>>
>

Re: [shoes] Buttons array

From:
Elias Crespin
Date:
2010-12-24 @ 19:18
Thanks Kevin,

Thanks for your point. It is very interesting and it clearly affects
behavior, but it still does not tell me how to access the index (that was
assigned to each button) from within  the click block.

The code displays niceley a grid of buttons, but the alert does not receive
the i value :( .

Bests and happy 24 dec!

Elias Crespin.


On Fri, Dec 24, 2010 at 7:28 PM, Kevin Ball <kmball11@gmail.com> wrote:

> I realized this may not have been one hundred percent clear...
>
> once you change the way you're assigning that variable, you should be able
> to use it within your button click block and have it retain its number.
>  Some sample code (just showing the principle, not using shoes):
>
> @b = []
> @c = []
>
> (0..210).each do |i|-
>   @b[i] = lambda {i}
> end
>
> for j in 0..210
>   @c[j] = lambda {j}
> end
>
> puts "@b[2]:  #{@b[2].call}"
> puts "@c[2]:  #{@c[2].call}"
>
>
> This results in
>
> @b[2]:  2
> @c[2]:  210
>
>
> On Fri, Dec 24, 2010 at 10:15 AM, Kevin Ball <kmball11@gmail.com> wrote:
>
>> I think the core question is how to get the value in i to stick around.
>>  The solution is to make i be a local variable that gets bound within the
>> closure of each block, rather than one that sticks around to change.
>>
>> Instead of using
>>
>> for i in 0..219
>>
>> try
>>
>> (0..219).each do |i|
>>
>> The difference is in the first one you have a single variable getting
>> assigned multiple values, and every block creates a closure referencing that
>> variable, so at the end of the loop they are all pointing at a variable that
>> is valued 219.
>>
>> In the second on, we create a new local variable i in each iteration
>> through the loop, so each button creates a closure referencing its own
>> variable.
>>
>> -Kevin
>>
>>
>> On Fri, Dec 24, 2010 at 10:03 AM, ashbb <ashbbb@gmail.com> wrote:
>>
>>> Hi Elias,
>>>
>>> Revised your code a little bit. ;-)
>>> Try this one.
>>>
>>> Shoes.app do
>>>   @b = []
>>>
>>>   for i in 0..219
>>>     @b[i] = button( "Pm"+i.to_s ){
>>>       alert "button clicked but how to know which index ?"
>>>     }
>>>     @b[i].move ((i/20)*60 ), ((i % 20)*20 + 1)
>>>   end
>>> end
>>>
>>> Hope this helps,
>>> ashbb
>>>
>>>
>>>
>>
>

Re: [shoes] Buttons array

From:
Kevin Ball
Date:
2010-12-24 @ 19:24
could not decode message

Re: [shoes] Buttons array

From:
Elias Crespin
Date:
2010-12-24 @ 19:31
Kevin,

Works like a charm!!

Thanks so much!! I passed more than 2 hours in front of it...

You just sent me my Christmas gift!

Happy new Year.
Elias Crespin.


On Fri, Dec 24, 2010 at 8:24 PM, Kevin Ball <kmball11@gmail.com> wrote:

> The attached code works on my mac with red shoes...
>
>
>
> On Fri, Dec 24, 2010 at 11:18 AM, Elias Crespin <elias.crespin@gmail.com>wrote:
>
>> Thanks Kevin,
>>
>> Thanks for your point. It is very interesting and it clearly affects
>> behavior, but it still does not tell me how to access the index (that was
>> assigned to each button) from within  the click block.
>>
>> The code displays niceley a grid of buttons, but the alert does not
>> receive the i value :( .
>>
>> Bests and happy 24 dec!
>>
>> Elias Crespin.
>>
>>
>> On Fri, Dec 24, 2010 at 7:28 PM, Kevin Ball <kmball11@gmail.com> wrote:
>>
>>> I realized this may not have been one hundred percent clear...
>>>
>>> once you change the way you're assigning that variable, you should be
>>> able to use it within your button click block and have it retain its number.
>>>  Some sample code (just showing the principle, not using shoes):
>>>
>>> @b = []
>>> @c = []
>>>
>>> (0..210).each do |i|-
>>>   @b[i] = lambda {i}
>>> end
>>>
>>> for j in 0..210
>>>   @c[j] = lambda {j}
>>>  end
>>>
>>> puts "@b[2]:  #{@b[2].call}"
>>> puts "@c[2]:  #{@c[2].call}"
>>>
>>>
>>> This results in
>>>
>>> @b[2]:  2
>>> @c[2]:  210
>>>
>>>
>>> On Fri, Dec 24, 2010 at 10:15 AM, Kevin Ball <kmball11@gmail.com> wrote:
>>>
>>>> I think the core question is how to get the value in i to stick around.
>>>>  The solution is to make i be a local variable that gets bound within the
>>>> closure of each block, rather than one that sticks around to change.
>>>>
>>>> Instead of using
>>>>
>>>> for i in 0..219
>>>>
>>>> try
>>>>
>>>> (0..219).each do |i|
>>>>
>>>> The difference is in the first one you have a single variable getting
>>>> assigned multiple values, and every block creates a closure referencing that
>>>> variable, so at the end of the loop they are all pointing at a variable that
>>>> is valued 219.
>>>>
>>>> In the second on, we create a new local variable i in each iteration
>>>> through the loop, so each button creates a closure referencing its own
>>>> variable.
>>>>
>>>> -Kevin
>>>>
>>>>
>>>> On Fri, Dec 24, 2010 at 10:03 AM, ashbb <ashbbb@gmail.com> wrote:
>>>>
>>>>> Hi Elias,
>>>>>
>>>>> Revised your code a little bit. ;-)
>>>>> Try this one.
>>>>>
>>>>> Shoes.app do
>>>>>   @b = []
>>>>>
>>>>>   for i in 0..219
>>>>>     @b[i] = button( "Pm"+i.to_s ){
>>>>>       alert "button clicked but how to know which index ?"
>>>>>     }
>>>>>     @b[i].move ((i/20)*60 ), ((i % 20)*20 + 1)
>>>>>   end
>>>>> end
>>>>>
>>>>> Hope this helps,
>>>>> ashbb
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>