Skip to content

Commit

Permalink
Add Object#peek as helper method to inspect object
Browse files Browse the repository at this point in the history
So, instead of you having to do this:

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

Now you can do:

  @user.peek.activate!
  • Loading branch information
sikachu committed Aug 3, 2012
1 parent 9abf589 commit 0e65330
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
1 change: 1 addition & 0 deletions activesupport/lib/active_support/core_ext/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 7 additions & 0 deletions activesupport/lib/active_support/core_ext/object/peek.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions activesupport/test/core_ext/object/peek_test.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions guides/source/active_support_core_extensions.textile
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down

0 comments on commit 0e65330

Please sign in to comment.