Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Diagnosing error in command line input

Reply
Thread Tools

Diagnosing error in command line input

 
 
Dave Thacker
Guest
Posts: n/a
 
      12-22-2007
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


 
Reply With Quote
 
 
 
 
Matthew Harris
Guest
Posts: n/a
 
      12-22-2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Diagnosing JS error that is not reported DrKen Javascript 8 11-30-2011 04:49 PM
Need help diagnosing intermittent Memory error when running RDLC reports andy6 via DotNetMonster.com ASP .Net 0 09-08-2006 03:15 PM
Diagnosing an application spawned from an ASP.NET web application James Tsao ASP .Net 1 10-29-2004 03:54 PM
Need help diagnosing a JVM crash on Linux Tony Java 0 10-15-2004 10:54 PM
Diagnosing Socket Error Roger Java 3 06-15-2004 02:20 PM



Advertisments