I have a program that looks like:
$offset = Point.new(100, 200);
def draw(point)
pointNew = $offset + point;
drawAbsolute(point)
end
draw(Point.new(3, 4));
the use of $offset
seems a bit weird.
In C, if I define something outside of any function, it is a global variable automatically. Why in Ruby does it have to be $offset
but cannot be offset
and still be global? If it is offset
, then it is a local? But local to where, because it feels very much global.
Are there better ways to write the code above? The use of $offset
may seem a bit ugly at first.
Update: I can put this offset inside a class
definition, but what if two or several classes need to use this constant? In this case do I still need to define an $offset
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…