Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
TkVariables and ".value" method
Alex DeCaria
3/8/2007 2:40:00 PM
The code below, which puts "Hello" in a label on press of a button,
works if the command for the button is { a.value = "Hello" }, but not if
it is { a = "Hello" }. I'm trying to understand "why", and can't find
anything on the ".value" method in in the Pragmatic Programmers' book on
Ruby.
Can someone explain why {a = "Hello"} won't work, and why I need to do
{a.value = "Hello"?}
Thanks in advance.
------------------------------------
require 'tk'
root = TkRoot.new.title("Example")
a = TkVariable.new
Labels = TkLabel.new(root) do
textvariable a
pack('side' => "top")
end
TkButton.new(root) do
text "Press"
command { a.value = "Hello" }
pack('side' => "top")
end
Tk.mainloop
----------------------------
--
Posted via
http://www.ruby-...
.
1 Answer
Brian Candler
3/8/2007 2:57:00 PM
0
On Thu, Mar 08, 2007 at 11:40:09PM +0900, Alex DeCaria wrote:
> The code below, which puts "Hello" in a label on press of a button,
> works if the command for the button is { a.value = "Hello" }, but not if
> it is { a = "Hello" }. I'm trying to understand "why", and can't find
> anything on the ".value" method in in the Pragmatic Programmers' book on
> Ruby.
http://rubycentral.com/book/tut_cl...
Scroll down to "Writable Attributes"
> Can someone explain why {a = "Hello"} won't work, and why I need to do
> {a.value = "Hello"?}
a = TkVariable.new # 'a' points to an instance of TkVariable
a.value = "Hello" # calls method 'value=' on this object,
# with "Hello" as the argument. Afterwards,
# 'a' still points to the same TkVariable object
BUT:
a = TkVariable.new # 'a' points to an instance of TkVariable
a = "Hello" # 'a' now points to a completely different object,
# which is a string. The TkVariable is not
# referenced from anywhere, and will be
# garbage-collected at some point
Try this in irb, see if it makes it any clearer:
class Foo
def bar=(x)
@q = x
end
def bar
@q
end
end
a = Foo.new
puts a.inspect
a.bar = 99
puts a.inspect
a = 123
puts a.inspect
Brian.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
TkVariables and ".value" method
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password