Class: Object

Inherits:
BasicObject
Defined in:
lib/egalite/blank.rb,
lib/egalite/support.rb

Overview

Tries to send the method only if object responds to it. Return nil otherwise.

Example :

# Without try With try @person.try(:name)

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

An object is blank if it’s nil, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are blank.

This simplifies

if !address.nil? && !address.empty?

to

if !address.blank?

Returns:

  • (Boolean)


12
13
14
# File 'lib/egalite/blank.rb', line 12

def blank?
  respond_to?(:empty?) ? empty? : !self
end

#try(method) ⇒ Object



31
32
33
# File 'lib/egalite/support.rb', line 31

def try(method)
  send(method) if respond_to?(method, true)
end