From: Chad Perrin Date: 2011-04-08T02:08:07+09:00 Subject: Re: String.gsub with regex and block --jho1yZJdad60DJr+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Apr 08, 2011 at 12:36:23AM +0900, Reid Thompson wrote: > On Fri, 2011-04-08 at 00:25 +0900, Alexey Petrushin wrote: > > "John Smith".gsub /(.+)\s(.+)/ do |name, family| > > p [name, family] > >=20 > > # instead of this > > p [$1, $2] > > end=20 > is it a requirement that you use gsub? >=20 > irb(main):008:0> name, family =3D "John Smith".split > =3D> ["John", "Smith"] > irb(main):009:0> p [name, family] > ["John", "Smith"] > =3D> nil This solution is not very generalizable. It only works as presented for cases where all the stuff you want to discard looks the same. I wouldn't want to have to deal with this kind of thing for a particularly complex case: str =3D 'John Smith: A Good Man -- A Good Husband. RIP (1976)'.split first, *remainder =3D str.split last, *non_name =3D remainder.split(': ') desc1, *the_rest =3D non_name.split(' -- ') desc2, *deceased =3D the_rest.split('. ') =2E . . so, is there some way to use more descriptive variable names than the default $1, $2, et cetera, for captures from within a regex? I'm not aware of any, but I too would find that agreeable. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --jho1yZJdad60DJr+ Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk2d67MACgkQ9mn/Pj01uKW2NwCfYHzCTUkUphCLButy5QsLDold M48AoKbmpqhKz600eXXxhb7++JZm8ZjL =pwH+ -----END PGP SIGNATURE----- --jho1yZJdad60DJr+--