Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

Changes:

* Breaking

* `Kernel#try!` now returns `nil` when the receiver doesn't respond to the
method (or it is private), instead of raising `NoMethodError`. `#try`
stays strict. This is deliberately the opposite of ActiveSupport, which
since Rails 4.0 makes `#try` lenient and `#try!` strict. In Ruby the bang
marks the more dangerous method, and quietly ignoring a missing method is
the dangerous behavior. The docs no longer claim ActiveSupport
compatibility.
* `Kernel#try` no longer calls private methods. It now behaves exactly like
a normal call or `&.`, raising `NoMethodError` for them.

* Enhancements

* New `lib/rails` library for Rails-compatible methods that Facets carries
Expand Down
39 changes: 25 additions & 14 deletions lib/core/facets/kernel/try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,46 @@
module Kernel

# Invokes the method identified by the symbol +method+, passing it any
# arguments and/or the block specified.
#
# Unlike regular send, a +NoMethodError+ exception will *not* be raised
# if the receiving object is +nil+ (see NilClass#try below).
#
# Compatible with ActiveSupport's #try, plus an additional Functor
# form when called with no arguments and no block.
# arguments and/or the block specified. If the receiver is +nil+, it
# returns +nil+ instead (see NilClass#try below). Otherwise it behaves
# exactly like a normal call (or &.): a method the receiver doesn't have,
# or a private one, raises NoMethodError.
#
# @example.try(:name) #=> "bob"
# @example.try { |o| o.name } #=> "bob" (ActiveSupport block form)
# @example.try.name #=> "bob" (Facets Functor form)
# @example.try { |o| o.name } #=> "bob"
# @example.try.name #=> "bob" (Functor form)
# nil.try.name #=> nil
#
# NOTE: THIS IS NOT ACTIVESUPPORT'S #try. SINCE RAILS 4.0, ACTIVESUPPORT'S
# #try RETURNS NIL FOR A METHOD THE RECEIVER DOESN'T HAVE, SO A TYPO LIKE
# user.try(:nmae) QUIETLY BECOMES NIL AND HIDES THE BUG. FACETS KEEPS #try
# STRICT AND PUTS THE LENIENT BEHAVIOR IN #try!, WHERE IT BELONGS: IN RUBY,
# THE BANG MARKS THE MORE DANGEROUS VERSION OF A METHOD, AND SWALLOWING A
# MISSING METHOD IS THE DANGEROUS ONE. RAILS HAS THE TWO BACKWARD.
#
def try(method=nil, *args, &block)
if method
__send__(method, *args, &block)
public_send(method, *args, &block)
elsif block_given?
yield self
else
self
end
end

# Like #try, but raises NoMethodError if the method doesn't exist
# (unless receiver is nil). Compatible with ActiveSupport's #try!.
# Like #try, but also returns +nil+ if the receiver doesn't respond to
# +method+ (or it is private), instead of raising NoMethodError.
#
# @example.try!(:name) #=> "bob"
# @example.try!(:nmae) #=> nil
#
# NOTE: THIS IS THE OPPOSITE OF ACTIVESUPPORT'S #try!, WHICH RAISES. THE
# BANG IS HERE BECAUSE QUIETLY IGNORING A MISSING METHOD IS THE DANGEROUS
# BEHAVIOR; SEE #try ABOVE.
#
def try!(method=nil, *args, &block)
if method
public_send(method, *args, &block)
public_send(method, *args, &block) if respond_to?(method)
elsif block_given?
yield self
else
Expand All @@ -40,7 +52,6 @@ def try!(method=nil, *args, &block)

end


class NilClass

# See Kernel#try.
Expand Down
37 changes: 37 additions & 0 deletions test/core/kernel/test_try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@
example.try.name.assert == "bob"
end

test "raises for a missing method" do
example = Struct.new(:name).new("bob")
NoMethodError.assert.raised? { example.try(:nmae) }
end

test "raises for a private method, like a normal call" do
example = Class.new { private def secret; :s; end }.new
NoMethodError.assert.raised? { example.try(:secret) }
end

end

method :try! do

test do
example = Struct.new(:name).new("bob")
example.try!(:name).assert == "bob"
end

test "returns nil for a missing method" do
example = Struct.new(:name).new("bob")
example.try!(:nmae).assert == nil
end

test "returns nil for a private method" do
example = Class.new { private def secret; :s; end }.new
example.try!(:secret).assert == nil
end

end

end
Expand All @@ -32,4 +61,12 @@

end

method :try! do

test do
nil.try!(:name).assert == nil
end

end

end
Loading