Class: NilClass

Inherits:
Object show all
Defined in:
lib/simple_ext/object/try.rb,
lib/simple_ext/object/blank.rb,
lib/simple_ext/object/to_query.rb

Instance Method Summary collapse

Instance Method Details

#blank?true

nil is blank:

nil.blank? # => true

Returns:

  • (true)


55
56
57
# File 'lib/simple_ext/object/blank.rb', line 55

def blank?
  true
end

#to_paramObject

Returns self.



20
21
22
# File 'lib/simple_ext/object/to_query.rb', line 20

def to_param
  self
end

#try(method_name = nil, *args) ⇒ Object

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

nil.try(:name) # => nil

Without try

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

With try

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


117
118
119
# File 'lib/simple_ext/object/try.rb', line 117

def try(method_name = nil, *args)
  nil
end

#try!(method_name = nil, *args) ⇒ Object

Calling try! on nil always returns nil.

nil.try!(:name) # => nil


124
125
126
# File 'lib/simple_ext/object/try.rb', line 124

def try!(method_name = nil, *args)
  nil
end