From: ptkwt@... (Phil Tomson) Date: 2003-09-28T13:50:53+09:00 Subject: Re: Thoughts on yield In article <87brt5wz83.fsf@ethereal.dyndns.org>, Nolan J. Darilek wrote: >I've begun working on a music-related ruby project, and recently I've >been pondering the idea of a music composition environment somewhat >similar to cm, clm and friends for lisp, but in ruby instead. > >Today I was thinking about how ruby blocks make constructing >domain-specific languages a snap. I've been reading Lisp as a Second >Language at http://www.ircam.fr/equipes/repmus/LispSecondLanguage/, a >Lisp tutorial for musicians, and was considering using a list-based >structure for storing music. I sorta envisioned the language as >looking something like: > >clef do > measure("4/4") do > c d e f g a b c > chord do > c e g > end > end >end > >In this example, a-g are methods returning note objects. > >What I'd like is to have that return a list like so: > >["c", "d", "e", "f", "g", "a", "b", "c", ["c", "e", "g"]] > I played with this problem for a few minutes and this is what I came up with: module Music def a(list=@notes) list << "a" end def b(list=@notes) list << "b" end def c(list=@notes) list << "c" end def d(list=@notes) list << "d" end def e(list=@notes) list << "e" end def f(list=@notes) list << "f" end def g(list=@notes) list << "g" end def clef puts "...In Clef..." yield return @notes end def measure(time) puts "...In Measure..." yield end def chord puts "...In Chord..." yield @chord_notes @notes << @chord_notes end def notes @notes end end class Song include Music def initialize @notes = [] @chord_notes=[] end end class MySong < Song include Music def initialize super clef do measure("4/4") do c; d; e; f; g; a; b; c; chord do |chrd| c(chrd); d(chrd); g(chrd); end end end end end mysong = MySong.new p mysong.notes Of course, the ugly bit is the part in the chord block: you have to pass in 'chrd' to all of the a-g methods to get those notes to be added to the correct list (@chord_list) which then gets appended to the @notes list. So it's still not quite what you want (I understand that you want people to see very little of the 'wizard behind the curtain' [Ruby] and having to pass 'chrd' sort of negates that). Hopefully, someone else will offer a suggestion for getting rid of that requirement. ....you may not like the Song class bit I added either... Phil