diff options
author | Vijay Dev <vijaydev.cse@gmail.com> | 2015-01-03 14:58:17 +0000 |
---|---|---|
committer | Vijay Dev <vijaydev.cse@gmail.com> | 2015-01-03 14:58:17 +0000 |
commit | 4b9dba99d65b1bd27576b16a68d7d18522bae9ea (patch) | |
tree | 4672dd2377a4e4ef71992c5478f2606f5d86e371 /activesupport | |
parent | 02e72a49d1c99084fef2c78c3a194e03b879099d (diff) | |
parent | 7cc145ec65167084e8b05d9462eaf94534fa0168 (diff) | |
download | rails-4b9dba99d65b1bd27576b16a68d7d18522bae9ea.tar.gz rails-4b9dba99d65b1bd27576b16a68d7d18522bae9ea.tar.bz2 rails-4b9dba99d65b1bd27576b16a68d7d18522bae9ea.zip |
Merge branch 'master' of github.com:rails/docrails
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/name_error.rb | 14 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/try.rb | 12 |
2 files changed, 23 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/name_error.rb b/activesupport/lib/active_support/core_ext/name_error.rb index e1ebd4f91c..b82148e4e5 100644 --- a/activesupport/lib/active_support/core_ext/name_error.rb +++ b/activesupport/lib/active_support/core_ext/name_error.rb @@ -1,5 +1,12 @@ class NameError # Extract the name of the missing constant from the exception message. + # + # begin + # HelloWorld + # rescue NameError => e + # e.missing_name + # end + # # => "HelloWorld" def missing_name if /undefined local variable or method/ !~ message $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message @@ -7,6 +14,13 @@ class NameError end # Was this exception raised because the given name was missing? + # + # begin + # HelloWorld + # rescue NameError => e + # e.missing_name?("HelloWorld") + # end + # # => true def missing_name?(name) if name.is_a? Symbol last_name = (missing_name || '').split('::').last diff --git a/activesupport/lib/active_support/core_ext/object/try.rb b/activesupport/lib/active_support/core_ext/object/try.rb index 26b8d58948..6b3fc48a3f 100644 --- a/activesupport/lib/active_support/core_ext/object/try.rb +++ b/activesupport/lib/active_support/core_ext/object/try.rb @@ -63,9 +63,12 @@ class Object try!(*a, &b) if a.empty? || respond_to?(a.first) end - # Same as #try, but will raise a NoMethodError exception if the receiver is not +nil+ and - # does not implement the tried method. - + # Same as #try, but will raise a NoMethodError exception if the receiver is + # not +nil+ and does not implement the tried method. + # + # "a".try!(:upcase) # => "A" + # nil.try!(:upcase) # => nil + # 123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Fixnum def try!(*a, &b) if a.empty? && block_given? if b.arity.zero? @@ -94,6 +97,9 @@ class NilClass nil end + # Calling +try!+ on +nil+ always returns +nil+. + # + # nil.try!(:name) # => nil def try!(*args) nil end |