Class: NilClass

Inherits:
Object show all
Defined in:
lib/more/facets/openobject.rb,
lib/core/facets/boolean.rb,
lib/more/facets/pathname.rb,
lib/core/facets/conversion.rb,
lib/core/facets/kernel/ergo.rb,
lib/core/facets/nilclass/status.rb

Overview

Core Extensions

Instance Method Summary collapse

Instance Method Details

#ergoObject

Compliments Kernel#ergo.

"a".ergo{ |o| o.upcase } #=> "A"
nil.ergo{ |o| o.bar } #=> nil

CREDIT: Daniel DeLorme


36
37
38
39
# File 'lib/core/facets/kernel/ergo.rb', line 36

def ergo
  @_ergo ||= Functor.new{ nil }
  @_ergo unless block_given?
end

#status(status = nil) ⇒ Object

Nil#status makes it possible to pass messages through a “failure” chain.

def foo
  return nil.status("failed foo!")
end

result = foo
if result.nil?
  result.status?  #=> true
  result.status   #=> "failed foo!"
end

CREDIT: Trans


18
19
20
21
22
23
24
25
# File 'lib/core/facets/nilclass/status.rb', line 18

def status(status=nil)
  if status
    @status = status
    self
  else
    @status
  end
end

#status?Boolean

Check status.

def foo
  return nil.status("failed foo!")
end

result = foo
if result.nil?
  result.status?  #=> true
  result.status   #=> "failed foo!"
end

CREDIT: Trans

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/core/facets/nilclass/status.rb', line 41

def status?
  return unless @status
  return false if @status.empty?
  return true
end

#to_boolObject



158
159
160
# File 'lib/core/facets/boolean.rb', line 158

def to_bool
  false
end

#to_fObject

Allows nil to respond to #to_f. Always returns 0.

nil.to_f   #=> 0.0

CREDIT: Matz


220
# File 'lib/core/facets/conversion.rb', line 220

def to_f; 0.0; end

#to_hObject

Allows nil to create an empty hash, similar to #to_a and #to_s.

nil.to_h    #=> {}

CREDIT: Trans


231
# File 'lib/core/facets/conversion.rb', line 231

def to_h; {}; end

#to_openobjectObject

Nil converts to an empty OpenObject.



207
208
209
# File 'lib/more/facets/openobject.rb', line 207

def to_openobject
  OpenObject.new
end

#to_pathObject

Provide platform dependent null path.

CREDIT Daniel Burger



117
118
119
# File 'lib/more/facets/pathname.rb', line 117

def to_path
  Pathname.null
end