Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/inactive_support/core_ext/object.rb
Instance Method Summary collapse
-
#ctry(*args) ⇒ Object
Chained try.
-
#identity ⇒ Object
Returns self.
Instance Method Details
#ctry(*args) ⇒ Object
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 dont have any arguments
14 15 16 17 18 19 20 21 |
# File 'lib/inactive_support/core_ext/object.rb', line 14 def ctry(*args) first, *rest = args if rest.any? self.try(first).ctry(*rest) else self.try(first) end end |
#identity ⇒ Object
Returns self. If given a block, it works a lot like Object#tap
Examples
[1,2,3,5,7].consecutive_by(:identity)
=> [[1, 2, 3], [5], [7]]
29 30 31 32 33 34 35 |
# File 'lib/inactive_support/core_ext/object.rb', line 29 def identity if block_given? yield self else self end end |