Created
November 20, 2011 19:09
-
-
Save benjaminplee/1380717 to your computer and use it in GitHub Desktop.
Playing with Ruby for QuickPiet interpreter
This file contains 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
#! /usr/bin/env ruby | |
class Recorder | |
def push amt | |
puts "push: #{amt}" | |
end | |
def add | |
puts "add" | |
end | |
def macro name, ¯o_def | |
puts "macro" | |
# first way; didn't work | |
# self.define_method name ¯o_def | |
# second way; works | |
self.class.send(:define_method, name.to_s, ¯o_def) | |
end | |
end | |
def quickpiet &script | |
recorder = Recorder.new | |
recorder.instance_eval &script | |
end | |
quickpiet do | |
macro plus2 do | |
push 2 | |
add | |
end | |
push 2 | |
plus2 | |
end |
Figured it out, error above was red herring b/c I didn't use a string or symbol for the macro method invocation.
next problem was accessing define_method correctly (As @adkron pointed out) since it is private to the class and not an instance method
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am looking for clean/simple way to expose quick piet commands w/o other ruby code and with a minimal added globally. Suggestions appreciated.
I was expecting this to output:
macro
push: 2
push: 2
add
instead I got
./piet.rb:27: undefined local variable or method
plus2' for #<Recorder:0x1001692d8> (NameError) from ./piet.rb:23:in
instance_eval'from ./piet.rb:23:in `quickpiet'
from ./piet.rb:26