Sha256: ca9d7c5281d69edcda4da222d6c0f2455685889db5a0ab1245acf2175fb536d6

Contents?: true

Size: 929 Bytes

Versions: 1

Compression:

Stored size: 929 Bytes

Contents

# NilConditional Operator in Ruby

class Object
  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
end

class NilConditional
  def method_missing(name, *params)
    return self.class.new
  end

  def nil?
    true
  end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nil_conditional-1.0.0 lib/nil_conditional.rb