Class: Object

Inherits:
BasicObject
Defined in:
lib/nil_conditional.rb

Overview

NilConditional Operator in Ruby

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nil_conditional.rb', line 4

def method_missing(name, *params)
  if name =~ /^__.+$/
    original = name.to_s.sub(/^__/, '').to_sym

    if block_given? && original =~ /^\w+$/
      binding = Proc.new.binding
      if binding.local_variable_defined?(original)
        var = binding.local_variable_get(original)
        return var unless var.nil?
      end
    end

    if respond_to?(original)
      return_value = block_given? ? send(original, *params, &Proc.new) : 
        send(original, *params)
      return return_value unless return_value.nil?
    end

    NilConditional.new
  else
    super
  end
end