Created
January 29, 2014 09:47
-
-
Save rutvij-pandya/8684740 to your computer and use it in GitHub Desktop.
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 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