Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

one.add.one

View 01-simple.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class Numeric
def add
LHS_Op.new self, :+
end
end
 
class LHS_Op
def initialize i, o
@lhs = i
@op = o
end
def one
@lhs.__send__ @op, 1
end
end
 
def one
1
end
 
p one
p one.add
p one.add.one
View 01-simple.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 58
class Calculator
 
def initialize i
@value = i
@operator = nil
end
 
def << x
if @operator
@value = self.evaluate x
@operator = nil
else
m = @value.method x
a = m.arity
if a > 1
raise "can't do multiple-arity operations"
else
@operator = x
if a == 0
@value = self.evaluate
@operator = nil
end
end
end
self
end
 
def evaluate *args
if @operator
@value.__send__ @operator, *args
elsif @operator
[@value, @operator]
else
@value
end
end
 
def to_s
self.evaluate.to_s
end
 
# Duplicate this for more number names
def one
self << 1
end
 
# Duplicate this for more operations
def add
self << :+
end
 
end
 
def one
Calculator.new 1
end
 
puts one.add.one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.