[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:04161] Regex question

From: hal9000@...
Date: 2000-07-21 16:38:14 UTC
List: ruby-talk #4161
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.

In This Thread

Prev Next