Created
October 26, 2012 13:20
-
-
Save mike-burns/3958781 to your computer and use it in GitHub Desktop.
refinements cannot do recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Literal < Struct.new(:number) | |
def show | |
print number | |
end | |
end | |
class Add < Struct.new(:a, :b) | |
def show | |
a.show | |
print ' + ' | |
b.show | |
end | |
end | |
module Evalution | |
refine Literal do | |
def eval | |
number | |
end | |
end | |
refine Add do | |
def eval | |
a.eval + b.eval | |
end | |
end | |
end | |
def run1 | |
using Evalution | |
Add.new(Literal.new(3), Literal.new(4)).show | |
puts | |
p Add.new(Literal.new(3), Literal.new(4)).eval | |
end | |
run1 | |
class Negate < Struct.new(:expression) | |
def show | |
print '- (' | |
expression.show | |
print ')' | |
end | |
end | |
module NegativeEvaluation | |
refine Negate do | |
def eval | |
- expression.eval | |
end | |
end | |
end | |
def run2 | |
using Evalution | |
using NegativeEvaluation | |
Add.new(Literal.new(5), Negate.new(Add.new(Literal.new(2), Literal.new(2)))).show | |
puts | |
p Add.new(Literal.new(5), Negate.new(Add.new(Literal.new(2), Literal.new(2)))).eval | |
end | |
run2 |
Author
mike-burns
commented
Oct 26, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment