From: Tobias Reif Date: 2002-07-09T04:09:46+09:00 Subject: Re: is there a better string.each? Marcin 'Qrczak' Kowalczyk wrote: > Mon, 8 Jul 2002 22:53:16 +0900, Albert Wagner pisze: > > >>Why not just subclass it to do what you like? Why change the >>language? Use the language, Luke. >> >>class Chapters < String >> : >>class Paragraphs < String >> : >> > Because they aren't differences between the data, but differences > between ways of processing of a string. It doesn't make much sense > for me to choose the iteration method when creating the string. absolutely. Perhaps class Document < String class Chapter < String class Section < String class Para < String p doc # => [document,[[section,[para,para]], [section[para,para,para]]]] Here's a prototype for s.th similiar I did a while ago: ----- # structured abstract model representation for the lib # utils: # # # # # # # class String def lines split "\n" end end class Array def from_2 self[1..-1] end end class Lib < Array def format map do |list| list.from_2.map do |item| item.join("\n") end end.join("\n\n") end end lib = Lib.new readme = '#!usr/local/bin/ruby # this file was automatically generated; and changes can get overwritten' lib.push ['readme',readme.lines] lib.push ['modules'],['classes'] class_ellipse = 'class Ellipse def cx 5 end def cx= 7 end end' class_rect = 'class Rect def x 5 end def x= 7 end end' module_id = 'module Id def id 9 end def id= 3 end end' [class_rect,class_ellipse].each do |class_code| lib.assoc('classes').push class_code.lines end #print all classes #print lib.assoc('classes').from_2.join "\n" # print class Ellipse #print lib.assoc('classes').assoc('class Ellipse').join("\n") lib.assoc('modules').push module_id.lines # prints out nicely open('generated_lib.tst','w') do |file| file.write lib.format end ----- Tobi -- http://www.pinkjuice.com/