N
Nathan Beyer
Given a simple class like this
class MyClass
attr_reader :value
def value=(val)
@value = val.to_s
end
end
Why the the following code return the value passed and not the value
assigned?
c = MyClass.new
result = c.value = 2
puts result
puts c.value
This is currently outputing
2
"2"
The results are the same when MyClass is modified to be this.
class MyClass
attr_reader :value
def value=(val)
@value = val.to_s
return @value
end
end
Shouldn't the result of the 'value=' method be the return value?
class MyClass
attr_reader :value
def value=(val)
@value = val.to_s
end
end
Why the the following code return the value passed and not the value
assigned?
c = MyClass.new
result = c.value = 2
puts result
puts c.value
This is currently outputing
2
"2"
The results are the same when MyClass is modified to be this.
class MyClass
attr_reader :value
def value=(val)
@value = val.to_s
return @value
end
end
Shouldn't the result of the 'value=' method be the return value?