Module: Stupidedi::Versions::FunctionalGroups::TwoThousandOne::ElementTypes::Operators::Binary

Included in:
FixnumVal::NonEmpty, FloatVal::NonEmpty
Defined in:
lib/stupidedi/versions/functional_groups/002001/element_types/operators.rb

Instance Method Summary collapse

Instance Method Details

#binary_operators(*ops) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/stupidedi/versions/functional_groups/002001/element_types/operators.rb', line 24

def binary_operators(*ops)
  file, line, = Stupidedi.caller

  if ops.last.is_a?(Hash)
    options = ops.pop

    case options[:coerce]
    when String, Symbol
      coerce = options[:coerce]
    else
      raise ArgumentError,
        "must pass :coerce => :method"
    end
  else
    raise ArgumentError,
      "must pass :coerce => :method"
  end

  ops.each do |op|
    class_eval(<<-RUBY, file, line.to_i - 1)
      def #{op}(other, &block)
        if other.respond_to?(:#{coerce})
          copy(:value => value.__send__(:#{op}, other.#{coerce}, &block))
        elsif other.respond_to?(:coerce)
          me, he = other.coerce(self)
          copy(:value => me.__send__(:#{op}, he, &block).#{coerce})
        else
          raise TypeError,
            "cannot coerce \#{other.class} to \#{self.class}"
        end
      end
    RUBY
  end
end