Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Forked from HeroicEric/gist:1763972
Created February 8, 2012 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save practicingruby/1764006 to your computer and use it in GitHub Desktop.
Save practicingruby/1764006 to your computer and use it in GitHub Desktop.
class Blog
attr_reader :entries
attr_writer :post_maker
def initialize
@entries = []
end
def title
"Underwater Basketweaving"
end
def subtitle
"The best way to spend your time, all the time"
end
def new_post
post_maker.call.tap do |p|
p.blog = self
end
end
private
def post_maker
@post_maker ||= Post.public_method(:new)
end
end
blog = Blog.new
MockPost = Struct.new(:blog)
blog.post_maker = -> { MockPost.new }
p1 = blog.new_post
p2 = blog.new_post
[p1, p2].each { |e| p [e.class, e.object_id] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment