Skip to content

Instantly share code, notes, and snippets.

@rutvij-pandya
Created January 29, 2014 09:47
Show Gist options
  • Save rutvij-pandya/8684740 to your computer and use it in GitHub Desktop.
Save rutvij-pandya/8684740 to your computer and use it in GitHub Desktop.
class Blog
# ...
attr_writer :post_source
# ...
private
def post_source
@post_source ||= Post.public_method(:new)
end
end
What is the advantage of using Post.public_method(:new) rather than
just Post.new?
The first one actually returns the method itself as an object, to be called later.  
The second is calling #new and returns a new Post object.
Thus this:
Post.new
is equivalent to this:
Post.public_method(:new).call
Another Example -
class Blog
def title
"Ruby DI"
end
end
foo = Blog.new
foo.title
>> "Ruby DI"
bar = foo.public_method(:title)
bar.call
>> "Ruby DI"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment