Conditional block operations

file.grep? I checked that grep is a method of Enumerable. is it suitable for
file? if so, it is a class method or instance method?

Tks.
Shannon

···

From: Emmanuel Touzery emmanuel.touzery@wanadoo.fr
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: Conditional block operations
Date: Tue, 3 Dec 2002 23:44:50 +0900

Emmanuel Touzery wrote:

Shannon Fang wrote:

I think for this case, the easist way might be

file.each do |line|
a, b = line.split(pattern) if line !~ /^\s*$/
end

grep could help, unfortunately I don’t know if it can be negated (eg “all
but the matches”).

emmanuel

actually \S is “non whitespace character”, so this is ok:

file.grep /^\S+$/ do |line|
a, b = line.split pattern
end

emmanuel

“If someone breaks into my house and steals my CDs,
who calls the cops, me or the music industry?
If it’s me, then that’s my property.” --Ruben Safir


Add photos to your e-mail with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail

file.grep? I checked that grep is a method of Enumerable. is it suitable for
file? if so, it is a class method or instance method?

Enumerable is included in IO

pigeon% ruby -e 'p IO.ancestors'
[IO, Enumerable, Object, Kernel]
pigeon%

Guy Decoux