Class: NilClass

Inherits:
Object show all
Defined in:
lib/ruby/blank.rb,
lib/ruby/try.rb

Overview

module Enumerable

# True if the collection is `empty?`
#
# @example
#   [1,2].blank?    #=> false
#   [].blank?       #=> false
#
unless respond_to?(:blank?)

def blank?
  empty?
end

def present?
  not empty?
end

end

Combinators collapse

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Always ‘true`. Note this overrides Object#blank? which returns false.

Examples:

nil.blank?    #=> true

Returns:

  • (Boolean)


45
46
47
# File 'lib/ruby/blank.rb', line 45

def blank?
  true
end

#present?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ruby/blank.rb', line 49

def present?
  false
end

#try(*args) ⇒ Object

Returns ‘nil` (when `self` is `nil`). This overrides Object#try

Examples:

"non-nil".try(&:length) #=> 7
nil.try(&:length)       #=> nil

"non-nil".try(:slice, 0, 3)   #=> "non"
nil.try(:slice, 0, 3)         #=> nil

Returns:

  • nil



42
43
44
# File 'lib/ruby/try.rb', line 42

def try(*args)
  self
end