ATLRUG - Cucumber
May 15th, 2009 Tim Kadom
Posted in Random | 1 Comment »
May 15th, 2009 Tim Kadom
Posted in Random | 1 Comment »
May 1st, 2009 tkadom
suppose you had the following code, What output would you expect to see?
#!/usr/bin/ruby
class RetTest
attr_reader :val
def val=(newval)
@val = newval < 100 ? newval : 100
end
def setval(newval)
@val = newval < 100 ? newval : 100
end
end
f = RetTest.new
g = RetTest.new
puts f.val= 120
puts f.val
puts g.setval 120
puts g.val
I thought it was gonna be turtles all the way down.
100 100 100 100
suprise to me the output was actually:
120 100 100 100.
Well to be clear, this was not the code that i originally started with, but its the code that illustrates a point. The setter in ruby actually does not do exactly what i would expect. I was thinking the last value assigned would be returned regardless. Something magical happens with the syntactic niceness that we have with our setters though. Instead of returning the value of the assignment, the rvalue is returned. I am not sure why this happens, and was wondering if anyone had any thoughts on the matter…
Posted in Random | 3 Comments »