From: Alexey Verkhovsky Date: 2005-02-16T06:54:32+09:00 Subject: Re: Ruby as a domain specific language? * cnmaclean@hotmail.com [2005-02-16 05:49:56 +0900]: >>In our current (proprietary) language, it's something like this: >> >>Define File 1234 >> Name = "My File" >> Parent = 5678 >> DataItem = "DataItem 1", Decimal, 10 bytes >> DataItem = "DataItem 2", Hex, 10 bytes >> >> Actually, quick and dirty domain language in Ruby is very easy. E.g., you can write in less than a day some Ruby that will take this: class File1234Parser < FileParser::Base define_file 1234, :name => 'My File', :Parent => 5678 { decimal "DataItem 1", 10.bytes hex "DataItem 2", 10.bytes } end File1234Parser.new(open('~/files/foo.1234')).parse ... or whatever... This would work by invoking a method FileParser::Base.define_file, that would take whatever arguments you give it, make some Ruby code out of it and then self.module_eval <<-EOL def parse end ... EOL For things like parsing and/or generating a flat, fixed-width file, this can be done in a day (I did it). I also used the same approach once to generate a test oracle for a fairly complicated billing application. The "price definition files" there looks like: class GprsPromotion << StandardPricePlan market_rates(:DEFAULT_PREPAID_RATES) rate :voice => flat_rate(1.00.ntd.per_minute, :rounding_factor => 15.seconds, :round => :up) rate :sms => event_rate(2.00.ntd.per_event) # etc end You can show it to someone who doesn't know Ruby and s/he will understand it. It is also very easy to add special behaviors, because you can define_file :Foo alias :generic_parse :parse def parse generic_parse do.some { ruby.wizardry } if necessary? end The dark side is that it all has to be valid Ruby syntax, and that is a bit more commas and braces than in the perfect world. -- Best regards, Alexey Verkhovsky Ruby Forum: http://ruby-forum.org (moderator) RForum: http://rforum.andreas-s.net (co-author) Instiki: http://instiki.org (maintainer)