From: Anton Bangratz Date: 2010-09-12T21:07:27+09:00 Subject: Re: #{} in xpath --------------enig9276B74E5AEE1C834B0C5370 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 09/12/2010 01:46 PM, Pen Ttt wrote: > code1 > data=3Ddoc.xpath('.//small//span[@id=3D"yfs_t10_aacc"]') > puts data.text >=20 > code2 > str=3D"aacc" > data=3Ddoc.xpath('.//small//span[@id=3D"yfs_t10_#{str}"]') > puts data.text >=20 > code1 can work,code2 can't,would you mind to tell me how to fix it? Trick is that the string interpolation is not working with single quotes, but only with double quotes. Compare: './/small//span[@id=3D"yfs_t10_#{str}"]' =3D> ".//small//span[@id=3D\"yfs_t10_\#{str}\"]" ".//small//span[@id=3D\"yfs_t10_#{str}\"]" =3D> ".//small//span[@id=3D\"yfs_t10_aacc\"]" Note the necessary escaping of the double quotes. There's also an alternative percent sign syntax for interpolated strings:= %{.//small//span[@id=3D"yfs_t10_#{str}"]} =3D> ".//small//span[@id=3D\"yfs_t10_aacc\"]" Regards, t. PS: Helpful documentation: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals --=20 Anton Bangratz - Key ID 363474D1 - http://tony.twincode.net/ fortune(6): Remembering is for those who have forgotten. -- Chinese proverb --------------enig9276B74E5AEE1C834B0C5370 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJMjMJ3AAoJEAgZMsE2NHTRZf8IAIFnzlv/tVQvOZbs/zxAxYNJ WFW+M90ZVxdN4zSd2hlqq8asjPD/b9GD6y7sgDcOHV5h9HhKDvLGfXFWVRglVdZN EXtEZ4YAMa5ddgiwYpkCFXXPQ91vF8C+BH9GcWkzWXnppUUl4Xd5ewwxt6qr2o4o xDHsgt0OARHIDnaO56uDTkCxRbV5Kc8Rs9XeGjw2Oj4cBqMlokcGMR8ULYjjFiad k7aa/uYfq5V1KRu9MZ0vv6van8ohfttzqExtfWUY97iinuG62Br1UYkjrN1hvtvg 9UFfP0YEI0qB3KqeALkKof712knKxOyuXXWyWoCB0LP0iEP+X0jM8iI0Buk92jk= =gKFD -----END PGP SIGNATURE----- --------------enig9276B74E5AEE1C834B0C5370--