Created
June 27, 2016 07:43
-
-
Save elct9620/35943a86f21ffde284552df4f9ad1c7d to your computer and use it in GitHub Desktop.
Ruby DSL - Polish Text
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
let number1 equal 1 plus 1 | |
let number2 equal 1 multiple 2 | |
show number1 plus number2 |
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 Caculate | |
def polish_text(definition_line) | |
polished_text = definition_line.clone | |
polished_text.gsub!(/let ([a-zA-Z0-9]+) /, '@\1') | |
polished_text.gsub!(/equal/, '=') | |
polished_text.gsub!(/plus/, '+') | |
polished_text.gsub!(/multiple/, '*') | |
polished_text.gsub!(/show /, 'puts ') | |
polished_text | |
end | |
def process(definition) | |
instance_eval polish_text(definition) | |
end | |
def method_missing(name, *args, &block) | |
instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}") | |
end | |
def execute | |
end | |
end | |
caculate = Caculate.new | |
File.open "dsl.txt" do |file| | |
while line = file.gets do | |
caculate.process(line) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment