Sha256: d68d07f2237d21bea45cec79c827beceda2b832ef7ca2ee3d4c8050821e01b7e
Contents?: true
Size: 988 Bytes
Versions: 2
Compression:
Stored size: 988 Bytes
Contents
class Object # Credit goes to the active_support contributors # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb # # Invokes the public method whose name goes as first argument just like # +public_send+ does, except that if the receiver does not respond to it the # call returns +nil+ rather than raising an exception. def try(*args, &b) if args.empty? && block_given? yield self else public_send(*args, &b) if respond_to?(args.first) end end # Chained try allows writing # # str.ctry(:mb_chars, :downcase, :dasherize) # # instead of # # str.try(:mb_chars).try(:downcase).try(:dasherize) # # Only works for methods that don't take arguments. def ctry(*args) first, *rest = args if rest.any? self.try(first).ctry(*rest) else self.try(first) end end end class NilClass def try(*args) nil end def ctry(*args) nil end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
inactive_support-1.0.0 | lib/inactive_support/object/try.rb |
inactive_support-0.1.0 | lib/inactive_support/object/try.rb |