Hi all.
I have found an error that does not understand. Look this simple code:
···
---------------------------------
class Foo
def value=(data)
puts "Look #{data}"
end
end
f = Foo.new.value = "at me" # => Look at me
---------------------------------
Great! A little assigner that works perfectly. But now I make a
constructor that use the assigner directly:
---------------------------------
class Foo
def initialize(data)
value = data
end
def value=(data)
puts "Look #{data}"
end
end
f = Foo.new("at me") # => Nothing!!!!!!!!!
---------------------------------
Why? The constructor doesn't use value= method. What is happening? Is
that normal?
Thanl you for your help.
--
DaVinci
You need an explicit self, ie. `self.value = data`. Without the `self`, Ruby
interprets that line as a local variable assignment, not a method call.
···
2009/8/27 David Espada <davinciSINSPAM@escomposlinuxagujero-negro.org>
Hi all.
I have found an error that does not understand. Look this simple code:
---------------------------------
class Foo
def value=(data)
puts "Look #{data}"
end
end
f = Foo.new.value = "at me" # => Look at me
---------------------------------
Great! A little assigner that works perfectly. But now I make a
constructor that use the assigner directly:
---------------------------------
class Foo
def initialize(data)
value = data
end
def value=(data)
puts "Look #{data}"
end
end
f = Foo.new("at me") # => Nothing!!!!!!!!!
---------------------------------
Why? The constructor doesn't use value= method. What is happening? Is
that normal?
--
James Coglan
http://jcoglan.com
It has sense. Other form, Ruby might search all methods of class in each
local assignment.
Thank you.
···
El jueves 27 de agosto, James Coglan escribió:
You need an explicit self, ie. `self.value = data`. Without the
`self`, Ruby interprets that line as a local variable assignment, not
a method call.
--
DaVinci