Conditional block operations

Hi,

From: “Roman Rytov” rrytov@entopia.com
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Conditional block operations
Date: Tue, 3 Dec 2002 22:58:15 +0900

Is there a way to filter results of EACH or COLLECT functions? Let’s say
in

file.each do |line|
a, b = line.split(pattern)
End

I think for this case, the easist way might be

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

But for the next one, I have no idea. I think it has something related to
array.each and array.each_index, because in collect, the |x| seems is the
element itself, not the index. For example.

myarray.collect {|x| x + ", "}

myarray.each_index do |i|
myarray[i]+=", " if i%2
end

Shannon

···

MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
http://join.msn.com/?page=features/virus

Shannon Fang wrote:

Hi,

From: “Roman Rytov” rrytov@entopia.com
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Conditional block operations
Date: Tue, 3 Dec 2002 22:58:15 +0900

Is there a way to filter results of EACH or COLLECT functions? Let’s say
in

file.each do |line|
a, b = line.split(pattern)
End

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

···


“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

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