Created
February 27, 2013 00:54
-
-
Save anonymous/5043892 to your computer and use it in GitHub Desktop.
Why does Post.print_author work? in order to get to the class method, shouldn't I be using Post::Post.print_author??
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post | |
class << self | |
def print_author | |
puts "The author of all posts is Jimmy" | |
end | |
end | |
end | |
Post.print_author | |
# "The author of all posts is Jimmy" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because:
class << self
def print_author
puts "The author of all posts is Jimmy"
end
end
Provides context for the print_author method. It's a way of avoiding using self.method_name for every class method of self, or, in this case, Post.