Afficher un message
Vieux 17/09/2007, 19h31   #8
Bertram Scharpf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Newbie: what's Ruby idiom for word-by-word input?

Hi,

Am Montag, 17. Sep 2007, 04:19:53 +0900 schrieb Ari Brown:
> On Sep 16, 2007, at 3:10 PM, Alex Shulgin wrote:
>> Please show me the Ruby way!

>
> Maybe you could try this:
>
> a = gets.chomp #=> "My name is Ari"
> words = a.split(/ /) #=> ["My", "name", "is", "Ari"]


This isn't actually elaborate as it doesn't recognize tabs
or multiple whitespace. It is even longer than

words = gets.split

With no argument or nil, String#split uses $; what normally
is nil, too. Then, split uses something like %r/[ \t\n\r\v\f]+/.

You may easily read whole lines; they shouldn't become too
long. Reading the first word before the user typed enter
would need to tweak terminal settings. Not worth the effort
in most cases.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

  Réponse avec citation
 
Page generated in 0,05852 seconds with 9 queries