Benjamints
Benjamints

Reputation: 849

Skipping until statement at certain number

I have a method which sings the 'bottles of beer' on the wall. I have a problem which is when it reaches '1' I want bottles to be singular and not plural.

I managed to do this by creating an 'if'. When @bottles reaches 1 this works. However, when the until statement reaches 1 it says

'Take one down, pass one round
one bottles of beer'

I need the until statement to skip this out at this point. How is this done?

'

 class BeerSong

  require 'humanize'

  attr_accessor :bottles

  def initialize(bottles)
  @bottles = bottles
  @bottles = 0 if @bottles < 0
  @bottles = 99 if @bottles > 99
  end

  public

  def print_song
  until @bottles == 0 do
   break if @bottles == 0
   puts "#{@bottles.humanize} bottles of beer on the wall"
   puts "#{@bottles.humanize} bottles of beer"
   puts "Take one down, pass one round"
   puts "#{(@bottles - 1).humanize} bottles of beer"
   @bottles = @bottles - 1
     if @bottles == 1
     puts "#{@bottle} bottle of beer on the wall"
     puts "#{@bottles.humanize} bottle of beer"
     puts "Take one down, pass one round"
     puts "#{(@bottles - 1).humanize} bottles of beer"
     @bottles = @bottles - 1
     end
    end
  end
end


beer = BeerSong.new(99)
beer.print_song

Upvotes: 0

Views: 28

Answers (1)

Gerry
Gerry

Reputation: 10507

You need to do an if/else so it will only print the singular form of the song when @bottles - 1 == 1, like so:

def print_song
  until @bottles == 0 do
    puts "#{@bottles.humanize} bottles of beer on the wall"
    puts "#{@bottles.humanize} bottles of beer"
    puts "Take one down, pass one round"

    if @bottles - 1 == 1
      puts "#{(@bottles - 1).humanize} bottle of beer"
    else
      puts "#{(@bottles - 1).humanize} bottles of beer"
    end

    @bottles = @bottles - 1

    if @bottles == 1
      puts "#{@bottle} bottle of beer on the wall"
      puts "#{@bottles.humanize} bottle of beer"
      puts "Take one down, pass one round"
      puts "#{(@bottles - 1).humanize} bottles of beer"
      @bottles = @bottles - 1
    end
  end
end

Although, there is no need to repeat the whole song for plural and singular, you can use a variable and change it (just like you do with @bottles) when you reach 1, for example, the print_song could be:

def print_song
  until @bottles == 0 do
    bottles = @bottles > 1 ? "bottles" : "bottle"
    puts "#{@bottles.humanize} #{bottles} of beer on the wall"
    puts "#{@bottles.humanize} #{bottles} of beer"
    puts "Take one down, pass one round"
    bottles = @bottles -1 == 1 ? "bottle" : "bottles"
    puts "#{(@bottles - 1).humanize}  #{bottles} of beer"
    @bottles -= 1
  end
end

Notice the use of a ternary here:

bottles = @bottles > 1 ? "bottles" : "bottle"

Which is the same as doing:

if @bottles > 1
  bottles = "bottles"
else
  bottles = "bottle"
end

And the use of -= here:

@bottles -= 1

Which is the same as doing:

@bottles = @bottles - 1

Upvotes: 1

Related Questions