In Ruby are there any related applications of the syntax: class << self ... end
Posted
by pez_dispenser
on Stack Overflow
See other posts from Stack Overflow
or by pez_dispenser
Published on 2009-05-22T05:30:01Z
Indexed on
2010/04/16
20:33 UTC
Read the original article
Hit count: 433
class << self
attr_accessor :n, :totalX, :totalY
end
The syntax above is used for defining class instance variables. But when I think about what syntax implies, it doesn't make any sense to me, so I'm wondering if this type of syntax is used for any other types of definitions. My point of confusion here is this:
class << self
The append operator normally means "add what's on the right to the object on the left". But in the context of this block, how does that add up to "put the contents of this block into the definition of the class instance rather than the instance"?
For the same reason I'm confused as to why in one context class << self can define class instance variables while in another it seems to create class variables such as here:
class Point
# Instance methods go here
class << self
# Class methods go here
end
end
© Stack Overflow or respective owner