On Mar 9, 2008, at 9:28 PM, Isaac Toothyxdip wrote:

> Wow this is VERY helpful and im going look over and run it (it looks
> like it has a menu button and then a box where you can draw a square,
> oval, a random scribble, and text.) But i have a question why can you
> just do a when for the opt_chosen variable to check it. I tried  
> this and
> it didnt work.

The opt_chosen local variable isn't in the right scope. That's why I  
do the instance_variable_set. It makes the TkVariable object referred  
to by opt_chosen available in a scope visible to the canvas.draw  
singleton method.

> Also is the instance_variable_set saying that @shape is the  
> variable and
> that opt_chosen is the value?

The instance variable @shape gets bound to the object which was  
originally bound to the local variable opt_chosen. After the call to  
instance_variable_set, both variables refer to the same TkVariable  
object. In Ruby it is important to be aware of the difference between  
variables and the objects they refer to.

> Can instance_variable_set be used on pretty much any tkclass?

The instance_variable_set method is a method of Object and is,  
therefore, inherited by all classes. But be careful with this method  
-- thoughtless use of it can cause real problems with your code. In  
my canvas example the use is safe because the instance variable is  
only added to one particular instance of TkCanvas so it can used in a  
method that is only available to that one particular instance.

> and for me doing this would i have to do the same method or something
> like with a input box or just simply a TkVariable?

I'm not sure I understand this question, but if you're asking is this  
the 'right' way to make objects visible inside methods defined for  
Ruby/Tk widgets, the answer is: There are many ways to solve scope  
and visibility problems. This is only one of them.

Regards, Morton