From: Robert Klemme Date: 2012-12-20T23:52:17+09:00 Subject: Re: Perl to Ruby: regex captures to assignment. On Thu, Dec 20, 2012 at 1:38 AM, Derrick B. wrote: > First of all, thanks for the fast responses! > > Secondly, I swear I tried bracketing the false side of the conditional > (pg 98, section 4.5.5.3 - "The Ruby Programming Language" book), but I > remember it assigning the array to the first variable - w. I would choose a completely different approach: I would have a single expression for matching and decide which assignments to make based on the value of one of the capturing groups in the conditional branch: ["1 3/4", "5"].each do |s| puts s if %r{\A(\d+)(?:\s+(\d+)/(\d+))?\z} =~ s w, n, d = $2 ? [$1.to_i, $2.to_i, $3.to_i] : [1, $1.to_i, 0] printf "%4d %4d %4d\n", w, n, d else $stderr.puts "No match #{s}" end end I also spiced the regexp a bit more to be more restrictive. > In Perl, there are times when "those brackety thingies" are not > necessary, but as a programmer, I like readabiliy (yes, still talking > about Perl :) ) and will include them when it just makes sense. If you like readability then why are you using Perl in the first place? :-) Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/