贝娄是我为ruby-kickstart课程创建课程的第一个(而且相当愚蠢的尝试)。
在我的新意儿中,这应该有效 - 但我得到了:
rubytest.rb:28:in `<main>': uninitialized constant BeerSong (NameError)
rb:28是&#34; def print_song&#34;
Class BeerSong
attr_accessor :beers
def initialize(beers)
beers = 0 if beers < 0
beers = 99 if beers > 99
self.beers = beers
end
def print_song
bottlecount = :beers
letters_to_numbers = Hash.new
letters_to_numbers = {
"Ninety" => 90,
"Eighty" => 80,
"Seventy" => 70,
"Sixty" => 60,
"Fifty" => 50,
"Fourty" => 40,
"Thirty" => 30,
"Twenty" => 20,
"Nineteen" => 19,
"Eightteen" => 18,
"Seventeen" => 17,
"Sixteen" => 16,
"Fifteen" => 15,
"Fourteen" => 14,
"Thirteen" => 13,
"Twelve" => 12,
"Eleven" => 11,
"Ten" => 10,
"nine" => 9,
"eight" => 8,
"seven" => 7,
"six" => 6,
"five" => 5,
"four" => 4,
"three" => 3,
"two" => 2,
"one" => 1
}
while bottlecount > 1
bottlecount_primerA = bottlecount.to_s
bottlecount_primerB = bottlecount_primerA[0].to_i*10
bottlecount_primerC = bottlecount_primerA[1].to_i
if bottlecount > 19 && bottlecount_primerC != 0
bottlecount_tens = letters_to_numbers.key(bottlecount_primerB)
bottlecount_singels = letters_to_numbers.key(bottlecount_primerC)
bottlecount_text = "#{bottlecount_tens}-#{bottlecount_singels}"
elsif bottlecount > 19 && bottlecount_primerC == 0
bottlecount_tens = letters_to_numbers.key(bottlecount_primerB)
bottlecount_text = "#{bottlecount_tens}"
elsif bottlecount > 9 && bottlecount < 20
bottlecount_text = letters_to_numbers.key(bottlecount)
end
puts "#{bottlecount_text} bottles of beer on the wall,"
puts "#{bottlecount_text} bottles of beer,"
puts "Take one down, pass it around,"
bottlecount -= 1
end
puts "one bottle of beer on the wall,"
puts "one bottle of beer,"
puts "Take one down, pass it around,"
puts "zero bottles of beer on the wall"
end
Bottleman = BeerSong.new (99)
Bottleman.print_song
一直在挠头,但不知道为什么班级不想去。
答案 0 :(得分:3)
class BeerSong
不仅错误地输入Class BeerSong
,而且end
语句未正确终止该类。所以Ruby试图找到BeerSong
的定义,而不是理解你正试图定义它。如果找不到它,它将返回未初始化的常量错误。
这种错误使得Rubyists对正确的缩进有所了解,因此很容易看到缺少end
语句之类的内容。