Skip to content

Instantly share code, notes, and snippets.

@elct9620
Created June 27, 2016 07:43
Show Gist options
  • Save elct9620/35943a86f21ffde284552df4f9ad1c7d to your computer and use it in GitHub Desktop.
Save elct9620/35943a86f21ffde284552df4f9ad1c7d to your computer and use it in GitHub Desktop.
Ruby DSL - Polish Text
let number1 equal 1 plus 1
let number2 equal 1 multiple 2
show number1 plus number2
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