NoMethodError: undefined method `scanf' for main:Object

A

Alex Shulgin

Hi,

I'm trying to use scanf(), but no luck:

$ ruby --version
ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
$ uname -a
Linux zrbite 2.6.21-2-686 #1 SMP Wed Jul 11 03:53:02 UTC 2007 i686 GNU/
Linux
$ irb
irb(main):001:0> w=scanf("%s")
NoMethodError: undefined method `scanf' for main:Object
from (irb):1
irb(main):002:0>

Trying to call $stdin.scanf() does not help either (NoMethodError).
What I'm missing?


Cheers,
Alex
 
B

Bil Kleb

Alex said:
Hi,
Hi.

Trying to call $stdin.scanf() does not help either (NoMethodError).
What I'm missing?

I don't know, but the ri docs sure are sparse for ruby 1.8.6 (2007-03-13):

$ ri scanf
More than one method matched your request. You can refine
your search by asking for information on one of:

IO#block_scanf, IO#scanf, Kernel#scanf, String#block_scanf,
String#scanf

$ ri Kernel.scanf
----------------------------------------------------------- Kernel#scanf
scanf(fs,&b)
------------------------------------------------------------------------
(no description...)

But the original project examples at

http://www.rubyhacker.com/code/scanf/

might get you going...

Regards,
 
D

dblack

Hi --

I don't know, but the ri docs sure are sparse for ruby 1.8.6 (2007-03-13):

$ ri scanf
More than one method matched your request. You can refine
your search by asking for information on one of:

IO#block_scanf, IO#scanf, Kernel#scanf, String#block_scanf,
String#scanf

$ ri Kernel.scanf
----------------------------------------------------------- Kernel#scanf
scanf(fs,&b)
------------------------------------------------------------------------
(no description...)

But the original project examples at

http://www.rubyhacker.com/code/scanf/

might get you going...

That version is obsolete; it's best to use the one that ships with
Ruby. You have to require it, though -- it's standard library rather
than core.

require 'scanf'
p "abc 123".scanf("%s%d") => ["abc", 123]

There's considerable documentation in the source file, but it's not in
rdoc form. It's on my to-do list....


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
D

dblack

Hi --

Hi,

I'm trying to use scanf(), but no luck:

$ ruby --version
ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
$ uname -a
Linux zrbite 2.6.21-2-686 #1 SMP Wed Jul 11 03:53:02 UTC 2007 i686 GNU/
Linux
$ irb
irb(main):001:0> w=scanf("%s")
NoMethodError: undefined method `scanf' for main:Object
from (irb):1
irb(main):002:0>

Trying to call $stdin.scanf() does not help either (NoMethodError).
What I'm missing?

You have to require it:

irb(main):001:0> require 'scanf'
=> true
irb(main):002:0> a = scanf("%s")
abc
=> ["abc"]
irb(main):003:0> a
=> ["abc"]


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
A

Alex Shulgin

But the original project examples at

might get you going...

That version is obsolete; it's best to use the one that ships with
Ruby. You have to require it, though -- it's standard library rather
than core.

require 'scanf'
p "abc 123".scanf("%s%d") => ["abc", 123]

There's considerable documentation in the source file, but it's not in
rdoc form. It's on my to-do list....

Thanks -- this resolves my issue. Seems like the docs on scanf are
simply outdated. :)


Alex
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top