Can anyone help me with this simple exercise?
class Item
def percents()
self * 100
end
end
answer = gets.chomp
puts answer.percents()
The result is:
percents.rb:7:in `<main>': undefined method `percents' for "300":String (NoMethodError)
Can anyone help me with this simple exercise?
class Item
def percents()
self * 100
end
end
answer = gets.chomp
puts answer.percents()
The result is:
percents.rb:7:in `<main>': undefined method `percents' for "300":String (NoMethodError)
Copyright © 2021 Jogjafile Inc.
The variable
answerneeds to be anItemobject in order to have thepercentsmethod. Or, you can remove thepercentsmethod from theItemclass, and have it take in an integer:This last line, however, won't do you as you would expect. Since
gets.chompreturns a string of your input, you'll be multiplying the string "300" by 100, which means your output will look like this:You can first convert your answer to an int, using
to_i, and then print the percentThere, that looks better. Now if you'd like make your
answeran object of classItem, that's a bit more tricky.Your output will look the same as above:
Let me know if you have further questions, as I'm not 100% sure of the intent of the program you're trying to write. I'd recommend checking out a few Ruby tutorials like Ruby in Twenty Minutes or Tutorial Point's ruby tutorial in your quest to learn Ruby. I hope this helps!