Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

5 participants

Prem Sichanugrist James Herdman Santiago Pastorino Jeremy Kemper Aaron Patterson
Prem Sichanugrist
Collaborator

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!
Prem Sichanugrist 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
James Herdman

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.

Santiago Pastorino
Owner

:-1: from me

Jeremy Kemper
Owner

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

Prem Sichanugrist
Collaborator

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
Aaron Patterson
Owner
Prem Sichanugrist
Collaborator

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

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

    Add Object#peek as helper method to inspect object

    sikachu authored
    So, instead of you having to do this:
    
      @user.tap{ |u| puts u.inspect }.activate!
    
    Now you can do:
    
      @user.peek.activate!
This page is out of date. Refresh to see the latest.
2  activesupport/CHANGELOG.md
View
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Add `Object#peek` as helper method to print out and inspect the object.
+
* Inflections can now be defined per locale. `singularize` and `pluralize` accept locale as an extra argument. *David Celis*
* `Object#try` will now return nil instead of raise a NoMethodError if the receiving object does not implement the method, but you can still get the old behavior by using the new `Object#try!` *DHH*
1  activesupport/lib/active_support/core_ext/object.rb
View
@@ -4,6 +4,7 @@
require 'active_support/core_ext/object/deep_dup'
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/object/inclusion'
+require 'active_support/core_ext/object/peek'
require 'active_support/core_ext/object/conversions'
require 'active_support/core_ext/object/instance_variables'
7 activesupport/lib/active_support/core_ext/object/peek.rb
View
@@ -0,0 +1,7 @@
+class Object
+ # Call +#tap+ on the object, then print out the inspect version of the object to your standard
+ # output. This is the shortcut for: +object.tap { |o| puts o.inspect }+
+ def peek
+ tap { |object| puts object.inspect }
+ end
+end
38 activesupport/test/core_ext/object/peek_test.rb
View
@@ -0,0 +1,38 @@
+require 'abstract_unit'
+require 'active_support/core_ext/object'
+
+class PeekTest < ActiveSupport::TestCase
+ class Sister
+ class << self
+ def inspect
+ "My Sister"
+ end
+
+ def cute?
+ false
+ end
+
+ def name
+ "Kirino"
+ end
+ end
+ end
+
+ def test_peek_on_nil
+ assert_equal "nil\n", capture(:stdout) { nil.peek }
+ end
+
+ def test_peek_on_class
+ assert_equal "My Sister\n", capture(:stdout) { Sister.peek }
+ end
+
+ def test_peek_on_string
+ assert_equal "\"Kirino\"\n", capture(:stdout) { Sister.name.peek }
+ end
+
+ def test_peek_chain
+ assert_equal "false\n", capture(:stdout) {
+ assert_equal "FALSE", Sister.cute?.peek.to_s.upcase
+ }
+ end
+end
9 guides/source/active_support_core_extensions.textile
View
@@ -498,6 +498,15 @@ Examples of +in?+:
NOTE: Defined in +active_support/core_ext/object/inclusion.rb+.
+h4. +peek+
+
+This method will print the inspect version of your object to the standard output. It's very useful for debugging your object. This method will return +self+, so it's chainable.
+
+<ruby>
+object.peek # Same as object.tap{ |o| puts o.inspect }
+object.peek.do_something # Same as object.tap{ |o| puts o.inspect }.do_somethign
+</ruby>
+
h3. Extensions to +Module+
h4. +alias_method_chain+
Something went wrong with that request. Please try again.