I have a script started but am receiving an error message. I typically have the right idea, but incorrect syntax or formatting.
Here are the exact instructions given:
Extend the Integer class by adding a method called to_oct which returns a string representing the integer in octal. We'll discuss the algorithm in class. Prompt the user for a number and output the octal string returned by to_oct.
Add another method to your Integer extension named "to_base". This method should take a parameter indicating the base that the number should be converted to. For example, to convert the number 5 to binary, I would call 5.to_base(2). This would return "101". Assume that the input parameter for to_base is an integer less than 10. to_base should return a string representing the decimal in the requested number base.
while n > 0
n = n/b
puts "Enter a number: "
tryagain.rb:16:in `<main>': undefined method `to_base' for "5":String (NoMethodError)
As suggested, do something like this:
class Integer def to_base b to_s b #same as self.to_s(b) end def to_oct to_base 8 #same as self.to_base(8) end end 5.to_base 2 #=> "101" 65.to_oct #=> "101"