A bizarre problem with data= methods

D

David Espada

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.
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]

2009/8/27 David Espada said:
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!!!!!!!!!



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.
 
S

Stefano Crocco

|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.
|

Because when ruby sees something like:

identifier = value

it thinks you're creating a local variable called identifier, not calling the
identifier= method. To force ruby to understand you want to call the method,
you need to use the dot notation, self.identifier = value, or use send:
send:)identifier=, value).

I hope this helps

Stefano
 
D

David Espada

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.

It has sense. Other form, Ruby might search all methods of class in each
local assignment.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top