Diagnosing error in command line input

Discussion in 'Ruby' started by Dave Thacker, Dec 22, 2007.

  1. Dave Thacker

    Dave Thacker Guest

    I'm working on a program (my first) that will need to read user input (a file
    name) from the command line. While looking for examples of reading command
    line input, I stumbled across this example(1) which I've cut and pasted, to
    see how it works.
    #!/usr/bin/ruby
    require 'readline'

    def prompt(prompt="> ")
    input = nil
    prompt += " " unless prompt =~ /\s$/
    loop do
    input = Readline.readline(prompt, true)
    break if input.length > 0
    end
    return input
    end

    apples = prompt("how many apples do you have?")
    pears = prompt("how many pears do you have?")
    nonsense = prompt("try my input history (up/down arrow)")

    printf "there are %d items in our input history\n",
    Readline::HISTORY

    When I run this I get:
    dthacker@buckbeak:~/learning/ruby$ ./test-cl-io.rb
    how many apples do you have? 3
    how many pears do you have? 7
    try my input history (up/down arrow) 5
    /test-cl-io.rb:18:in `printf': can't convert Object into Integer (TypeError)
    from ./test-cl-io.rb:18

    I'm guessing that HISTORY is not the method I want to call. Is that my
    problem? Or is there a simple newbie syntax error I'm making?
    TIA
    Dave

    (1)http://beaver.net/slides/ruby/10-easy-pieces.html
    Dave Thacker, Dec 22, 2007
    #1
    1. Advertising

  2. Dave Thacker wrote:
    > I'm working on a program (my first) that will need to read user input (a file
    > name) from the command line. While looking for examples of reading command
    > line input, I stumbled across this example(1) which I've cut and pasted, to
    > see how it works.
    > #!/usr/bin/ruby
    > require 'readline'
    >
    > def prompt(prompt="> ")
    > input = nil
    > prompt += " " unless prompt =~ /\s$/
    > loop do
    > input = Readline.readline(prompt, true)
    > break if input.length > 0
    > end
    > return input
    > end
    >
    > apples = prompt("how many apples do you have?")
    > pears = prompt("how many pears do you have?")
    > nonsense = prompt("try my input history (up/down arrow)")
    >
    > printf "there are %d items in our input history\n",
    > Readline::HISTORY
    >
    > When I run this I get:
    > dthacker@buckbeak:~/learning/ruby$ ./test-cl-io.rb
    > how many apples do you have? 3
    > how many pears do you have? 7
    > try my input history (up/down arrow) 5
    > ./test-cl-io.rb:18:in `printf': can't convert Object into Integer (TypeError)
    > from ./test-cl-io.rb:18
    >
    > I'm guessing that HISTORY is not the method I want to call. Is that my
    > problem? Or is there a simple newbie syntax error I'm making?
    > TIA
    > Dave
    >
    > (1)http://beaver.net/slides/ruby/10-easy-pieces.html
    >
    >
    >
    >

    The object pointed to by Readline::HISTORY is not an object that can be
    converted to an Integer. Your printf attempts to convert it to an
    integer because it has %d. The Readline::HISTORY object is closer to an
    array.

    Try the following:

    printf "there are %d items in our input history\n", Readline::HISTORY.size


    --
    Matthew Harris
    http://matthewharris.org
    Matthew Harris, Dec 22, 2007
    #2
    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. James Tsao
    Replies:
    1
    Views:
    354
    bruce barker
    Oct 29, 2004
  2. Roger

    Diagnosing Socket Error

    Roger, Jun 14, 2004, in forum: Java
    Replies:
    3
    Views:
    467
    Sudsy
    Jun 15, 2004
  3. Tony
    Replies:
    0
    Views:
    594
  4. andy6 via DotNetMonster.com
    Replies:
    0
    Views:
    685
    andy6 via DotNetMonster.com
    Sep 8, 2006
  5. DrKen
    Replies:
    8
    Views:
    542
    John G Harris
    Nov 30, 2011
Loading...

Share This Page