From: Matthew Kerwin Date: 2012-12-20T08:30:42+09:00 Subject: Re: Perl to Ruby: regex captures to assignment. --f46d042f9690dc575f04d13cae67 Content-Type: text/plain; charset=ISO-8859-1 As it is, the parser is interpreting the ternary operation as the first item in the list, like: tmp = frac_str.match(/\//) \ ? frac_str.match(/(?:(\S+) )*?(\S+)\/(\S+)/).captures \ : 0 w, n, d = tmp, frac_str, d Subsequently puts(tmp) is calling Array#to_s which concatenates the elements, so you see "134" I got it to work by turning the third parameter of the ternary operation into an explicit array: w, n, d = frac_str.match(/\//) \ ? frac_str.match(/(?:(\S+) )*?(\S+)\/(\S+)/).captures \ : [0, frac_str, 1] # <= !!! puts "w: #{w}, n: #{n}, d: #{d}" # check assignment Outputs: w: 1, n: 3, d: 4 -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ ABN: 59-013-727-651 "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd --f46d042f9690dc575f04d13cae67 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
As it is, the parser is interpreting the ternary operation= as the first item in the list, like:

=A0 =A0 tmp =3D=A0= frac_str.match(/\//) \
=A0 =A0 =A0 ? frac_str.match(/(?:(\S+) )*?(\S+)\= /(\S+)/).captures \
=A0 =A0 =A0 : 0
=A0 =A0 w, n, d =3D tmp, frac_str, d

Subsequently puts(tmp) is calling Array#t= o_s which concatenates the elements, so you see "134"
<= br>
I got it to work by turning the third parameter of the ternary operati= on into an explicit array:

=A0 =A0 w, n, d =3D frac= _str.match(/\//) \
=A0 =A0 =A0 ? frac_str.match(/(?:(\S+) )*?(\S+= )\/(\S+)/).captures \
=A0 =A0 =A0 : [0, frac_str, 1] =A0# <=3D !!!
=A0 =A0 puts= "w: #{w}, n: #{n}, d: #{d}" =A0# check assignment

Outputs: =A0w: 1, n: 3, d: 4

--
=A0 Matthew Kerwin, B.Sc (CompSci) (Hons)
=A0 http://matthew.kerwin.net.au/<= /a>
=A0 ABN: 59-013-727-651

=A0 "You'll never find a pro= gramming language that frees
=A0 you from the burden of clarifying your ideas." - xkcd
--f46d042f9690dc575f04d13cae67--