Benx Shen's personal annotations on this page
-
class Strawberry
def Strawberry.color
return "red"
enddef self.size
return "kinda small"
endclass << self
def shape
return "strawberry-ish"
end
end
end
Strawberry.color # -> "red"
Strawberry.size # -> "kinda small"
Strawberry.shape # -> "strawberry-ish"Note the three different constructions: ClassName.method_name and self.method_name are essentially the same - outside of a method definition in a class block, self refers to the class itself. The latter is preferred, as it makes changing the name of the class much easier. The last construction, class << self, puts us in the context of the class's "meta-class" (sometimes called the "eigenclass")
This link has been bookmarked by 1 people . It was first bookmarked on 24 Oct 2009, by Benx Shen.
-
-
class Strawberry
def Strawberry.color
return "red"
enddef self.size
return "kinda small"
endclass << self
def shape
return "strawberry-ish"
end
end
end
Strawberry.color # -> "red"
Strawberry.size # -> "kinda small"
Strawberry.shape # -> "strawberry-ish"Note the three different constructions: ClassName.method_name and self.method_name are essentially the same - outside of a method definition in a class block, self refers to the class itself. The latter is preferred, as it makes changing the name of the class much easier. The last construction, class << self, puts us in the context of the class's "meta-class" (sometimes called the "eigenclass")
-
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.