Skip to content

new has_a block

Pretty simple post, but any ruby object can be post conifged in a line with somthing like:

User.new(:name => “tony”){|u| u.save}

which is great for active record tests.

Or in a bigger example:

@trip = Trip.new{|t| t.save!}
@invite1 = Invitation.new(:user => users(:aaron),
                                   :status => Invitation::STATUS_MAYBE){|i| i.save}
@invite1 = Invitation.new(:user => users(:aaron),
                                   :status => Invitation::STATUS_MAYBE){|i| i.save}

2 Comments

  1. Tom wrote:

    Why not just use Invitation#create instead?

    Sunday, September 28, 2008 at 4:32 am | Permalink
  2. admin wrote:

    Good point. A poor choice of examples. I just found it interesting and it illustrates that many methods that you wouldn’t expect have blocks.

    Sunday, September 28, 2008 at 10:32 am | Permalink

Post a Comment

Your email is never published nor shared.