Class: Object

Inherits:
BasicObject
Defined in:
lib/babushka/core_patches/try.rb,
lib/babushka/core_patches/object.rb

Overview

The implementation of #try as found in activesupport:

lib/active_support/core_ext/object/try.rb

Try is just a nil-swallowing send, which means return nil when called on nil and just send like normal when called on any other object.

Instance Method Summary collapse

Instance Method Details

#metaclassObject

Return this object’s metaclass; i.e. the value of self within a ‘class << self’ block.



6
7
8
# File 'lib/babushka/core_patches/object.rb', line 6

def metaclass
  class << self; self end
end

#tap {|_self| ... } ⇒ Object

Return this object after yielding it to the block. This is useful for neatly working with an object in some way before returning it:

def valmorphanize
  process.tap {|result|
    log_error "Oops!" unless result
  }
end

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



17
18
19
20
# File 'lib/babushka/core_patches/object.rb', line 17

def tap &block
  yield self
  self
end

#tappObject

Return self unmodified after logging the output of #inspect, along with the point at which tapp was called.



24
25
26
# File 'lib/babushka/core_patches/object.rb', line 24

def tapp
  tap { puts "#{File.basename caller[2]}: #{self.inspect}" }
end