Working through the classes section in the O’reilley book and they seem to be indicating the below should work:
class Point
def initialize(x,y)
@x, @y = x, y
end
def x
@x
end
def y
@y
end
def to_s
"(#@x,#@y)"
end
end
p = Point.new(5,0)
q = Point.new(p.x*2, p.y*2)
q.x = 0
puts q.x
In theory, I’m expecting it to print 0, instead my compiler is returning a NoMethodError upon trying to perform q.x = 0. Anything jumping out at you guys?
That code should not work, as there is no method
x=defined (as per the error message). There might be an errata online?