PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > NoMethodError: undefined method `scanf' for main:Object
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
NoMethodError: undefined method `scanf' for main:Object

Réponse
 
LinkBack Outils de la discussion
Vieux 16/09/2007, 09h21   #1 (permalink)
Alex Shulgin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut NoMethodError: undefined method `scanf' for main:Object

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 either (NoMethodError).
What I'm missing?


Cheers,
Alex

  Réponse avec citation
Vieux 16/09/2007, 13h05   #2 (permalink)
Bil Kleb
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: NoMethodError: undefined method `scanf' for main:Object

Alex Shulgin wrote:
> Hi,


Hi.

> Trying to call $stdin.scanf() does not 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,
--
Bil Kleb
http://nasarb.rubyforge.org
  Réponse avec citation
Vieux 16/09/2007, 13h15   #3 (permalink)
dblack@wobblini.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: NoMethodError: undefined method `scanf' for main:Object

Hi --

On Sun, 16 Sep 2007, Bil Kleb wrote:

> Alex Shulgin wrote:
>> Hi,

>
> Hi.
>
>> Trying to call $stdin.scanf() does not 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...


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)

  Réponse avec citation
Vieux 16/09/2007, 13h16   #4 (permalink)
dblack@wobblini.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: NoMethodError: undefined method `scanf' for main:Object

Hi --

On Sun, 16 Sep 2007, Alex Shulgin wrote:

> 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 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)

  Réponse avec citation
Vieux 16/09/2007, 13h27   #5 (permalink)
Alex Shulgin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: NoMethodError: undefined method `scanf' for main:Object

On Sep 16, 3:15 pm, dbl...@wobblini.net wrote:
>
> > 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....


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


Alex

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h43.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12319 seconds with 13 queries