下面的代碼旨在取整數,每個數字平方,並返回整數與平方數字。Ruby:未定義的方法`數字'爲3212:Fixnum(NoMethodError)
不過,我一直有這個錯誤:
`square_digits': undefined method `digits' for 3212:Fixnum (NoMethodError)
from `
'
我不明白爲什麼我有這個錯誤的.digits方法是在Ruby中包含的方法,我用它上一個整數,但它給了我一個NoMethodError。
def square_digits(digit)
puts digit
puts digit.inspect
puts digit.class
if digit <= 0
return 0
else
#converts the digit into digits in array
split_digit = digit.digits
puts "split digit class and inspect"
puts split_digit.inspect
puts split_digit.class
# multiples each digit by itself
squared = split_digit.map{ |a| a * a}
squared.reverse!
# makes digits into string
string = squared.join('')
# converts string into integer
string.to_i
end
end
有誰知道發生了什麼事?
你使用的是什麼版本的Ruby? AFAIK'整數#位數'來自Ruby 2.4(最新版本)。 –
我在我的計算機上使用了最新版本,但代碼正在另一臺計算機上運行。我不知道他們的紅寶石的版本。這可能是問題所在。 – alucinare