Class: NilClass

Inherits:
Object show all
Defined in:
lib/buff/extensions/object/try.rb,
lib/buff/extensions/object/blank.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

nil is blank:

nil.blank? # => true

Returns:

  • (Boolean)

47
48
49
# File 'lib/buff/extensions/object/blank.rb', line 47

def blank?
  true
end

#try(*args) ⇒ Object

Calling try on nil always returns nil. It becomes specially helpful when navigating through associations that may return nil.

nil.try(:name) # => nil

Without try

@person && !@person.children.blank? && @person.children.first.name

With try

@person.try(:children).try(:first).try(:name)

74
75
76
# File 'lib/buff/extensions/object/try.rb', line 74

def try(*args)
  nil
end

#try!(*args) ⇒ Object


78
79
80
# File 'lib/buff/extensions/object/try.rb', line 78

def try!(*args)
  nil
end