Forum: Ruby `&' interpreted as argument prefix

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
19eb75164135659a8fae98101b1c250e?d=identicon&s=25 Arun Kumar (arun_nss)
on 2009-03-16 05:16
Hi,
I'm new to rubi and at present i'm trying to write a sample code for
html extraction and store it in the database. The following is the code
for that :

doc = Hpricot(open("http://www.google.com/"))
(doc/"a").each do |link|
   if (link.attributes['class'] == 'gb1')
      href =  link.inner_text.strip
        con = DBI.connect("DBI:Mysql:Sample:localhost", "arunkumar",
"123456")
        stat = con.prepare("Insert into hello values ('', ?, 'GK', 71,
'female')")
        stat.execute("#{href}")
        stat.finish
        con.commit
        puts "Records have been inserted"
    else
        puts "Sorry! No matches found."
    end
end

When i execute it all works well except a warning saying :
/usr/lib/ruby/gems/1.8/gems/hpricot-0.6.164/lib/hpricot/builder.rb:26:
warning: `&' interpreted as argument prefix

I dont know where went wrong for such a warning to get displayed. Please
help.

Regards
Arun Kumar
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-03-16 07:33
(Received via mailing list)
On Mar 15, 2009, at 21:14 , Arun Kumar wrote:

>        con = DBI.connect("DBI:Mysql:Sample:localhost", "arunkumar",
> end
>
> When i execute it all works well except a warning saying :
> /usr/lib/ruby/gems/1.8/gems/hpricot-0.6.164/lib/hpricot/builder.rb:26:
> warning: `&' interpreted as argument prefix

that's from hpricot and won't change anytime soon :/

you may want to switch to mechanize instead. Kill two birds with one
stone.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-16 08:35
(Received via mailing list)
Arun Kumar wrote:
> Hi,
> I'm new to rubi and at present i'm trying to write a sample code for
> html extraction and store it in the database. The following is the code
> for that :
>
> doc = Hpricot(open("http://www.google.com/"))

The offending line is:

       ele.instance_variable_set("@#{k}", v)

Apparently Google pages contain very complex HTML, to relieve strain on
their
servers. Then, Hpricot does not "sanitize" its input. That k variable
might
contain a &, which Ruby then warns about. instance_variable_set()
creates an
instance variable, like this:

    @foo = v

where 'foo' was in k. But if k contains '&foo', you get this:

    @&foo = v

You can't write that in raw Ruby, so instance_variable_set() is warning
you that
you should not write it in "meta-programming" Ruby either.

But none of this is your fault: It's a bug in Hpricot, which Google's
advanced
HTML uncovered.

The conclusion: Switch to Nokogiri. It has an Hpricot compatibility
mode, but
its internal engine is libxml, which is one of the industry's leading
XML (and
therefor HTML) implementations.
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-03-16 09:19
(Received via mailing list)
2009/3/16 Phlip <phlip2005@gmail.com>:
>
>      ele.instance_variable_set("@#{k}", v)
>
Well, I don't think so.

The offending line is:
ele.instance_eval &blk

If you modify it to
ele.instance_eval(&blk)
The warning is gone.

Regards,

Park Heesob
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-03-16 09:32
(Received via mailing list)
On Mar 16, 2009, at 01:16 , Heesob Park wrote:

> Well, I don't think so.
>
> The offending line is:
> ele.instance_eval &blk
>
> If you modify it to
> ele.instance_eval(&blk)
> The warning is gone.

yup yup...
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-03-16 16:40
(Received via mailing list)
On Mon, 16 Mar 2009 03:16:57 -0500, Heesob Park wrote:

>> The offending line is:
> The warning is gone.
>
> Regards,
>
> Park Heesob

Looks fixed if you get Hpricot version 0.6.211 or later.

http://github.com/why/hpricot/commit/
aa2b51651bee9b3770bc2ecb8f1886b47aee8584
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-03-16 20:16
(Received via mailing list)
On Mar 16, 2009, at 08:37 , Ken Bloom wrote:

> Looks fixed if you get Hpricot version 0.6.211 or later.
>
> http://github.com/why/hpricot/commit/
> aa2b51651bee9b3770bc2ecb8f1886b47aee8584

I've said it before and I'll say it again: It isn't fixed until it is
released.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-03-16 21:20
Ryan Davis wrote:
> > warning: `&' interpreted as argument prefix
>
> you may want to switch to mechanize instead. Kill two birds with one
> stone.

Why two? Getting rid of this bug is one bird. Where's the second?
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-03-17 02:10
(Received via mailing list)
On Mon, 16 Mar 2009 14:12:17 -0500, Ryan Davis wrote:

> On Mar 16, 2009, at 08:37 , Ken Bloom wrote:
>
>> Looks fixed if you get Hpricot version 0.6.211 or later.
>>
>> http://github.com/why/hpricot/commit/
>> aa2b51651bee9b3770bc2ecb8f1886b47aee8584
>
> I've said it before and I'll say it again: It isn't fixed until it is
> released.

OK. Somehow I thought that assigning a version number meant it was a
release.
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-03-18 02:40
(Received via mailing list)
On Mon, 16 Mar 2009 19:42:00 -0500, Ken Bloom wrote:

>> released.
>
> OK. Somehow I thought that assigning a version number meant it was a
> release.

Today it was actually released, and an 0.7 gem is now on RubyForge.

--Ken
This topic is locked and can not be replied to.