Last active
December 14, 2015 19:59
-
-
Save nixpulvis/5140741 to your computer and use it in GitHub Desktop.
Generate ruby classes from text files.
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
data = File.read('classes.txt') | |
classes = data.split(/^$\n/) | |
classes.each do |klass| | |
name, *methods = klass.split(/:?\n/) | |
Object.const_set(name, Class.new do | |
methods.each do |method| | |
parse = method.match(/(?<name>\w+) \(?(?<arguments>[\w\, ]*)\)? ?-> {(?<body>.*)}/) | |
if parse[:arguments].empty? | |
define_method(parse[:name]) { eval parse[:body] } | |
else | |
arguments = parse[:arguments].split(', ') | |
define_method(parse[:name]) do |*args| | |
eigenclass = class << self; self; end | |
arguments.each_with_index do |arg, i| | |
eigenclass.class_eval { attr_accessor arg } | |
eval "self.#{arg} = :#{args[i]}" | |
end | |
eval parse[:body] | |
end | |
end | |
end | |
end) | |
end |
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
Foo: | |
math -> { 1 + 3 } | |
strings (a, b) -> { "#{a} and #{b}" } | |
Bar: | |
google -> { `curl www.google.com` } | |
funny -> { google.gsub("Lucky", "Funny") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment