Morton Goldberg
4/8/2007 2:02:00 AM
On Apr 7, 2007, at 3:18 PM, Alex DeCaria wrote:
> If I put a symbol into a TkVariable, it returns a string. The
> following
> code
> illustrates this:
>
> require 'tk'
> a = TkVariable.new
> a.value = :hi
> puts a.value == :hi
> puts a.value == "hi"
>
> This results in
>
> "false"
> "true"
>
> My question is, "Is this what's supposed to happen, or is this a bug?"
It's not a bug. A TkVariable object doesn't maintain a @value
instance variable. So when you call TkVariable#value= and pass it a
symbol, the symbol will have to be coerced into something that the
underlying tcl/tk interpreter understands, which is a string. When
you call TkVariable#value, the string is retrieved from the tcl/tk
interpreter, but your TkVariable object has no memory of how the
value originated at this point.
To see how TkVariable handles different kinds of values, you can
evaluate code similar to the following:
<code>
require "tk"
v = TkVariable.new(:foo)
v.value # => "foo"
v = TkVariable.new([1, 2, 3])
v.value # => "1 2 3"
v = TkVariable.new(:foo => "red", :bar => "green")
v.value # => {"foo"=>"red", "bar"=>"green"}
</code>
Note that hashes are handled specially. Personally, I have not
encountered a situation which needed a TkVariable object that was
built on a hash, but I'm sure such situations exist. Indeed, I would
be interested if someone would post something instructive about such
situations.
Regards, Morton