From: hal9000@... Date: 2000-07-21T16:38:14+00:00 Subject: [ruby-talk:04161] Regex question The program here is totally frivolous -- it prints out the obnoxious drinking song "99 Bottles of Beer on the Wall." But the regex question is real. I'm not doing the substituting the "proper" way -- a block with \1 \2 and so on. Would someone care to improve this? Of course, comments unrelated to the regex are welcome also. But it's not really intended to be elegant or efficient. :) Thanks, Hal -------------- class Inebriation What="N bottle of beer" Where=" on the wall" How="Take one down and pass it around,\n" Verse = What + Where + ",\n" + What + ";\n" + How + What + Where + "! \n\n" def drink(*brew) brew.each do |x| cold_one = Verse.sub /N/,x.to_s cold_one.sub! /N/,x.to_s cold_one.sub! /N/,(x-1).to_s cold_one.sub! /bottle /,"bottles " unless x == 1 cold_one.sub! /bottle /,"bottles " unless x == 1 regex = Regexp.new("#{x-1} bottle ") cold_one.sub! regex,"#{x-1} bottles " unless (x-1) == 1 print cold_one end end end blitzkrieg = Inebriation.new beer=(1..99).to_a.reverse! blitzkrieg.drink *beer -------------- -- Hal Fulton Sent via Deja.com http://www.deja.com/ Before you buy.