Class: NilClass

Inherits:
Object show all
Defined in:
lib/ruby/commons/core_ext/object/try.rb,
lib/ruby/commons/core_ext/object/blank.rb,
lib/ruby/commons/core_ext/object/duplicable.rb

Instance Method Summary collapse

Instance Method Details

#mp_blank?true

nil is blank:

nil.mp_blank? # => true

Returns:

  • (true)


56
57
58
# File 'lib/ruby/commons/core_ext/object/blank.rb', line 56

def mp_blank?
  true
end

#mp_duplicable?Boolean

nil is not duplicable:

nil.mp_duplicable? # => false
nil.dup            # => TypeError: can't dup NilClass

Returns:

  • (Boolean)


35
36
37
# File 'lib/ruby/commons/core_ext/object/duplicable.rb', line 35

def mp_duplicable?
  false
end

#mp_try(*args) ⇒ Object

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

nil.mp_try(:name) # => nil

Without mp_try

@person && @person.children.any? && @person.children.first.name

With mp_try

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


95
96
97
# File 'lib/ruby/commons/core_ext/object/try.rb', line 95

def mp_try(*args)
  nil
end

#mp_try!(*args) ⇒ Object



99
100
101
# File 'lib/ruby/commons/core_ext/object/try.rb', line 99

def mp_try!(*args)
  nil
end