Ruby one liner killed my script

Discussion in 'Ruby' started by John Maclean, Feb 12, 2008.

  1. John Maclean

    John Maclean Guest

    Talk about redundancy!

    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


    --

    Regards,

    John Maclean


    --

    Regards,

    John Maclean
    MSc (DIC)
    +44 7739 171 531
     
    John Maclean, Feb 12, 2008
    #1
    1. Advertising

  2. John Maclean

    7stud -- Guest

    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
    >


    Does this help:

    input = "10000001"
    puts input.to_i(base=2)

    --output:--
    129
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 12, 2008
    #2
    1. Advertising

  3. John Maclean

    John Maclean Guest

    Where can I find out more about that method within ri?
    ri Object?

    On Tue, 12 Feb 2008 09:50:25 +0900
    7stud -- <> 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
    > >

    >
    > Does this help:
    >
    > input = "10000001"
    > puts input.to_i(base=2)
    >
    > --output:--
    > 129



    --

    Regards,

    John Maclean
    MSc (DIC)
    +44 7739 171 531
     
    John Maclean, Feb 12, 2008
    #3
  4. John Maclean

    Peña, Botp Guest

    From: John Maclean [mailto:]=20
    # Where can I find out more about that method within ri?
    # ri Object?

    judging fr stud's reply, maybe you can try the string object first, eg,

    botp@pc4all:~$ qri to_i
    ------------------------------------------------------ Multiple choices:

    Class#to_i, Float#to_i, IO#to_i, IPAddr#to_i, Integer#to_i,
    NilClass#to_i, Process::Status#to_i, Rational#to_i, String#to_i,
    Symbol#to_i, Time#to_i

    botp@pc4all:~$ qri string.to_i
    ------------------------------------------------------------ String#to_i
    str.to_i(base=3D10) =3D> integer
    ------------------------------------------------------------------------
    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.

    "12345".to_i #=3D> 12345
    "99 red balloons".to_i #=3D> 99
    "0a".to_i #=3D> 0
    "0a".to_i(16) #=3D> 10
    "hello".to_i #=3D> 0
    "1100101".to_i(2) #=3D> 101
    "1100101".to_i(8) #=3D> 294977
    "1100101".to_i(10) #=3D> 1100101
    "1100101".to_i(16) #=3D> 17826049

    botp@pc4all:~$

    kind regards -botp
     
    Peña, Botp, Feb 12, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kev Jackson

    ruby one-liner needed

    Kev Jackson, Dec 2, 2005, in forum: Ruby
    Replies:
    8
    Views:
    122
    David A. Black
    Dec 2, 2005
  2. Alain Helfenstein

    Ruby Command Line One Liner

    Alain Helfenstein, Jan 1, 2009, in forum: Ruby
    Replies:
    5
    Views:
    142
    Alain Helfenstein
    Jan 11, 2009
  3. kj
    Replies:
    4
    Views:
    97
    Michele Dondi
    May 26, 2004
  4. Larry
    Replies:
    1
    Views:
    102
    Martien Verbruggen
    Feb 3, 2005
  5. Simon O
    Replies:
    8
    Views:
    171
    Mumia W.
    Jun 15, 2006
Loading...

Share This Page