Skip to content

Instantly share code, notes, and snippets.

@ttakezawa
Created May 23, 2011 05:24
Show Gist options
  • Save ttakezawa/986261 to your computer and use it in GitHub Desktop.
Save ttakezawa/986261 to your computer and use it in GitHub Desktop.
basic eRuby implementation
#!/usr/bin/ruby
# via [ruby-list:48093]
def eRuby(eruby_source)
source = eruby_source
ruby = ''
loop do
break if source.empty?
if source=~/<%=/m
text = $`
if text != ""
ruby << "print(#{text.dump})\n"
end
source = $'
if source=~/%>/m
code = $`
if code != ""
ruby << "print(#{code})\n"
end
source=$'
next
else
raise "<%= unmatch error"
end
end
if source=~/<%/m
text = $`
if text != ""
ruby << "print(#{text.dump})\n"
end
source = $'
if source=~/%>/m
code = $`
if code != ""
ruby << code << "\n"
end
source=$'
next
else
raise "<% unmatch error"
end
end
ruby << "print(#{source.dump})\n"
break
end
ruby
end
if $0==__FILE__
print eRuby(STDIN.read)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment