Reputation: 1031
I'm using named rexeg capture groups and my case statement works with match
, but it gives me data that I don't want. When I run the code below it only works to match one statement. Where am I going wrong?
File::open(file).lines do |line|
case
when line.scan(regex1) then puts line.scan(regex1)
when line.scan(regex2) then puts line.scan(regex2)
when line.scan(regex3) then puts line.scan(regex3)
end
end
end
Upvotes: 0
Views: 605
Reputation: 27855
case
executes the first true-expresion.
If you have multiple checks, where each check can be true, you should use mutliple if
-statements.
File::open(file).lines do |line|
puts line.scan(regex1) if line.scan(regex1)
puts line.scan(regex2) if line.scan(regex2)
puts line.scan(regex3) if line.scan(regex3)
end
I think the following version is a bit more flexible and efficient:
File::open(file).lines do |line|
[ regex1, regex2, regex3] do |regex|
if result = line.scan(regex)
puts result
end
end
end
Upvotes: 4