Class: NilConditional

Inherits:
Object show all
Defined in:
lib/nil_conditional.rb

Overview

Nil Conditional class

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ NilConditional

Returns a new instance of NilConditional.



12
13
14
# File 'lib/nil_conditional.rb', line 12

def initialize(object)
  @object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/nil_conditional.rb', line 16

def method_missing(name, *params)
  if @object.respond_to?(name)
    return_value = nil
    if block_given?
      return_value = @object.public_send(name, *params, &Proc.new)
    else
      return_value = @object.public_send(name, *params)
    end
    return return_value unless return_value.nil?
  end
  self.class.new(@object)
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
# File 'lib/nil_conditional.rb', line 33

def ==(other)
  return true if other.nil?
  super
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/nil_conditional.rb', line 38

def eql?(other)
  return true if other.eql?(nil)
  super
end

#nil?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/nil_conditional.rb', line 29

def nil?
  true
end