I spent some time with irb with the aim of getting an understanding of
methods and class building. I originally wanted to convert a binary
number to denary, with input from the user.
# strike one!
ruby -e 'p 0b10010101111'
does it on one line.
Anyway, I still looked at the few lines that I had and I'm wondering
how I can convert a user's input into a fixnum?
class B2d
def initialize @ustring = " "
end
def get_bin_string
p 'binary numbers : ' @ustring = gets.chomp
end
def test_bin_string
if @ustring =~ /[a-z]|[2-9]|[A-Z]| /
p "binary digits only and no spaces, please!"
exit 1
end
end
def put_bin_string
# this is the line in which I originally tried to convert the user's
binary digit to denary puts "your bin #{@ustring.to_i} =
0b#{@ustring.to_i}" end
end
t = B2d.new
t.get_bin_string
t.test_bin_string
t.put_bin_string
Where can I find out more about that method within ri?
ri Object?
···
On Tue, 12 Feb 2008 09:50:25 +0900 7stud -- <bbxx789_05ss@yahoo.com> wrote:
John Maclean wrote:
> I'm wondering
> how I can convert a user's input into a fixnum?
>
> def put_bin_string
> # this is the line in which I originally tried to convert
> # the user's binary digit to denary:
>
>puts "your bin #{@ustring.to_i} =
> 0b#{@ustring.to_i}" end
> end
>
From: John Maclean [mailto:info@jayeola.org]
------------------------------------------------------------------------
Returns the result of interpreting leading characters in str as an
integer base base (2, 8, 10, or 16). Extraneous characters past
the end of a valid number are ignored. If there is not a valid
number at the start of str, 0 is returned. This method never
raises an exception.