| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

10gbRubyTeacher

Page history last edited by Mike Leishman 16 years, 10 months ago

10 Green Bottles in Ruby

Courtesy Leon Brooks

 

 

#!/usr/bin/ruby

10.downto(1) do |b|

print b," green bottles, sitting on the wall.\n"

print "If 1 green bottle should accidentally fall,\n"

print "there'd be ",b-1," green bottles, "

print "sitting on the wall.\n\n"

end

Produces...

$ ruby ten-bottles.rb

10 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 9 green bottles, sitting on the wall.

9 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 8 green bottles, sitting on the wall.

8 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 7 green bottles, sitting on the wall.

7 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 6 green bottles, sitting on the wall.

6 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 5 green bottles, sitting on the wall.

5 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 4 green bottles, sitting on the wall.

4 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 3 green bottles, sitting on the wall.

3 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 2 green bottles, sitting on the wall.

2 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 1 green bottles, sitting on the wall.

1 green bottles, sitting on the wall.

If 1 green bottle should accidentally fall, there'd be 0 green bottles, sitting on the wall.

$

 

To include plurals

 

#!/usr/bin/ruby

10.downto(1) do |b|

print b," green bottle",(b==1)?"":"s",", sitting on the wall.\n"

print "If 1 green bottle should accidentally fall,\n"

print "there'd be ",b-1," green bottle",(b==2)?"":"s"

print ", sitting on the wall.\n\n"

end

 

A bit more complex and using words in place of numbers

 

#!/usr/bin/ruby

n = [ "no", "one", "two", "three", "four", "five",

"six", "seven", "eight", "nine", "ten" ]

10.downto(1) do |b|

print n[b]," green bottles, sitting on the wall.\n"

print "If ",n[1]," green bottle should accidentally fall,\n"

print "there'd be ",n[b-1]," green bottles, "

print "sitting on the wall.\n\n"

end

Produces...

$ ruby ten-bottles-english.rb

ten green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be nine green bottles, sitting on the wall.

nine green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be eight green bottles, sitting on the wall.

eight green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be seven green bottles, sitting on the wall.

seven green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be six green bottles, sitting on the wall.

six green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be five green bottles, sitting on the wall.

five green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be four green bottles, sitting on the wall.

four green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be three green bottles, sitting on the wall.

three green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be two green bottles, sitting on the wall.

two green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be one green bottles, sitting on the wall.

one green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be no green bottles, sitting on the wall.

$

If you wanted to make that fancy, you could uppercase the first letter when printing out a number at the start of a sentence (print n[b].capitalize, " green...") or ramp it up a few bytes by adding a text-to-speech facility... something to keep your students busy for a few months...

 

Even  more!

 

This time, instead of using an iterator to loop through the numbers, we use a set of numbers:

 

#!/usr/bin/ruby

n = [ "no", "one", "two", "three", "four", "five",

"six", "seven", "eight", "nine", "ten" ] nb = [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ];

nb.each do |b|

print n[b]," green bottles, sitting on the wall.\n"

print "If ",n[1]," green bottle should accidentally fall,\n"

print "there'd be ",n[b-1]," green bottles, "

print "sitting on the wall.\n\n"

end

 

OBTW, because these files have the interpreter identified on the first line (#!/usr/bin/ruby), it's possible to flag them as executable (chmod a+x ten-bottles-array.rb) and then just run the script itself, letting the shell sort out what to use to run it:

$ ./ten-bottles-array.rb

ten green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be nine green bottles, sitting on the wall.

nine green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be eight green bottles, sitting on the wall.

eight green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be seven green bottles, sitting on the wall.

seven green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be six green bottles, sitting on the wall.

six green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be five green bottles, sitting on the wall.

five green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be four green bottles, sitting on the wall.

four green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be three green bottles, sitting on the wall.

three green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be two green bottles, sitting on the wall.

two green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be one green bottles, sitting on the wall.

one green bottles, sitting on the wall.

If one green bottle should accidentally fall, there'd be no green bottles, sitting on the wall.

$

The "./" is needed because by default your current directory isn't in your PATH. However...

$ echo $PATH

/usr/bin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin/:/usr/games:/usr/lib/qt3/bin:/home/leonb/bin:/usr/lib/qt3/bin

...copying the Ruby file to somewhere like /usr/games would put it in the PATH, so a plain ten-bottles-array.rb would work.

 

 

 

 

Comments (0)

You don't have permission to comment on this page.