class Object
I stole it from ActiveSupport library of Ruby on Rails (MIT License)
Tries to send the method only if object responds to it. Return nil
otherwise.
Example :¶ ↑
# Without try @person ? @person.name : nil
With try @person.try(:name)
Public Instance Methods
blank?()
click to toggle source
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?
# File lib/egalite/blank.rb, line 12 def blank? respond_to?(:empty?) ? empty? : !self end
try(method)
click to toggle source
# File lib/egalite/support.rb, line 31 def try(method) send(method) if respond_to?(method, true) end