Module: Puppeteer::IfPresent

Instance Method Summary collapse

Instance Method Details

#if_present(target, &block) ⇒ Object

Similar to #try in ActiveSupport::CoreExt.

Evaluate block with the target, only if target is not nil. Returns nil if target is nil.


if_present(params) do |target|

Point.new(target['x'], target['y'])

end


Raises:

  • (ArgumentError)


12
13
14
15
16
17
# File 'lib/puppeteer/if_present.rb', line 12

def if_present(target, &block)
  raise ArgumentError.new('block must be provided for #if_present') if block.nil?
  return nil if target.nil?

  block.call(target)
end