Class: NilClass

Inherits:
Object show all
Defined in:
lib/active_support/core_ext/object/try.rb,
lib/active_support/core_ext/object/json.rb,
lib/active_support/core_ext/object/blank.rb,
lib/active_support/core_ext/object/to_query.rb,
lib/active_support/core_ext/object/duplicable.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object

:nodoc:



80
81
82
# File 'lib/active_support/core_ext/object/json.rb', line 80

def as_json(options = nil) #:nodoc:
  self
end

#blank?true

nil is blank:

nil.blank? # => true

Returns:

  • (true)


57
58
59
# File 'lib/active_support/core_ext/object/blank.rb', line 57

def blank?
  true
end

#to_paramObject

Returns self.



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

def to_param
  self
end

#try(*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)


138
139
140
# File 'lib/active_support/core_ext/object/try.rb', line 138

def try(*args)
  nil
end

#try!(*args) ⇒ Object

Calling try! on nil always returns nil.

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


145
146
147
# File 'lib/active_support/core_ext/object/try.rb', line 145

def try!(*args)
  nil
end