Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG.md
@@ -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
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
@@ -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
@@ -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
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