Add Object#peek as helper method to inspect object #7243

Closed
wants to merge 1 commit into from

5 participants

@sikachu
Ruby on Rails member

This method is very helpful when inspecting and debug the code. So, instead of you having to do this:

@user.tap{ |u| puts u.inspect }.activate!

Now you can do:

@user.peek.activate!
@sikachu sikachu Add Object#peek as helper method to inspect object
So, instead of you having to do this:

  @user.tap{ |u| puts u.inspect }.activate!

Now you can do:

  @user.peek.activate!
0e65330
@jherdman

Maybe I'm just really naive, but why do we need this? I can honestly say I've never used this pattern before. I'd love to hear more about its use case.

@spastorino
Ruby on Rails member

👎 from me

@jeremy
Ruby on Rails member

I do this sometimes, but rarely with puts. Doesn't merit a method on Object IMO.

@sikachu
Ruby on Rails member

I think it's just a gimmick for saving you some time. So, instead of having to fix the whole line, or type .tap { |o| puts o.inspect }, I can just call .peek, run the test, and i'll see the "#inspect" result.

So, these are more use cases:

User.active.where(:created_at => 10.days.ago)

If I want to print out active users for debugging purpose, instead of having to:

User.active.tap{ |u| puts u.inspect }.where(:created_at => 10.days.ago)

I'd just

User.active.peek.where(:created_at => 10.days.ago)

Also, adding #peek at the end of the line when it'll get assigned to the variable is easy:

@users = User.active.where(:created_at => 10.days.ago).peek
# instead of
@users = User.active.where(:created_at => 10.days.ago)
puts @users.inspect
@tenderlove
Ruby on Rails member
@sikachu
Ruby on Rails member

Thanks for the review guys. I was thinking this would be helpful, but seems like no one uses it (and has a bad method name.) I couldn't think of the better name though.

Anyway, closing this for now. If anybody has a different opinion feel free to reopen/merge it. Thanks. :D

@sikachu sikachu closed this Aug 3, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment