This post originated from an RSS feed registered with Ruby Buzz
by Jeremy Voorhis.
Original Post: Genetic Madlibs
Feed Title: JVoorhis
Feed URL: http://feeds.feedburner.com/jvoorhis
Feed Description: JVoorhis is a Rubyist in northeast Ohio. He rambles about Ruby on Rails, development practices, other frameworks such as Django, and on other days he is just full of snark.
This example may not be very interesting for those familiar with evolutionary programming techniques, but it created a fun diversion, highlights how the DRP library is to be used, and shows a different solution for a language generator.
class DrpGenerator
extend DRP::RuleEngine
begin_rules
def main() "#{hello}\n#{ex1}\n#{ex2}" end
def hello() "Hello, #{somebody}!" end
def somebody() "matz" end
def somebody() "world" end
def ex1() "This is a more #{adj1} #{example}." end
def adj1() "complex" end
def adj1() "elaborate" end
def example() "example" end
def example() "test" end
def ex2() "Some simple sentence." end
def ex2() "Another, involving harder stuff." end
def ex2() "Another, involving a more complex #{exp}." end
def ex2() "Yet another possibility; each one is chosen with an evolutionary algorithm." end
def exp() "expression" end
def exp() "disjunction" end
def exp() example end
end_rules
end
g = DrpGenerator.new
3.times { puts g.main }
Hello, matz!
This is a more elaborate example.
Another, involving a more complex disjunction.
Hello, world!
This is a more elaborate test.
Another, involving harder stuff.
Hello, world!
This is a more complex example.
Yet another possibility; each one is chosen with an evolutionary algorithm.
My DrpGenerator isn’t as readable or attractive as the eigenclass DSL, and multiple definitions of the same method will likely confuse most Rubyists at first glance, but makes it easy to see the underlying grammar.