Skip to content

Instantly share code, notes, and snippets.

Created February 27, 2013 00:54
Show Gist options
  • Save anonymous/5043892 to your computer and use it in GitHub Desktop.
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??
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"
@LogaJ
Copy link

LogaJ commented Feb 27, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment