我想解決https://www.hackerrank.com/challenges/30-class-vs-instance但我得到一個錯誤。 我的代碼工作,只有當intialAge小於0類和實例
* solution.rb:13:amIOld': undefined method
<'的零:NilClass(NoMethodError)
class Person
attr_accessor :age
def initialize(initialAge)
if initialAge < 0
puts "Age is not valid, setting age to 0."
age = 0
else
initialAge = age
end
age
end
def amIOld()
if age < 13
puts "You are young."
elsif age.between?(13, 18)
puts "You are a teenager."
else
puts "You are old."
end
end
def yearPasses()
age += 1
end
end
您將參數設置爲'age',這是未定義的。這是倒退。 –
你的問題是什麼? – sawa
's/initialAge =年齡/年齡= initialAge /':) – mudasobwa