`&' interpreted as argument prefix

Discussion in 'Ruby' started by Arun Kumar, Mar 16, 2009.

  1. Arun Kumar

    Arun Kumar Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Arun Kumar, Mar 16, 2009
    #1
    1. Advertising

  2. Arun Kumar

    Ryan Davis Guest

    On Mar 15, 2009, at 21:14 , 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/"))
    > (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


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

    you may want to switch to mechanize instead. Kill two birds with one
    stone.
     
    Ryan Davis, Mar 16, 2009
    #2
    1. Advertising

  3. Arun Kumar

    Phlip Guest

    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.

    --
    Phlip
     
    Phlip, Mar 16, 2009
    #3
  4. Arun Kumar

    Heesob Park Guest

    2009/3/16 Phlip <>:
    > 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 =3D Hpricot(open("http://www.google.com/"))

    >
    > The offending line is:
    >
    > =C2=A0 =C2=A0 =C2=A0ele.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
     
    Heesob Park, Mar 16, 2009
    #4
  5. Arun Kumar

    Ryan Davis Guest

    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...
     
    Ryan Davis, Mar 16, 2009
    #5
  6. Arun Kumar

    Ryan Davis Guest

    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.
     
    Ryan Davis, Mar 16, 2009
    #6
  7. 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?
    --
    Posted via http://www.ruby-forum.com/.
     
    Albert Schlef, Mar 16, 2009
    #7
  8. Arun Kumar

    Ken Bloom Guest

    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.



    --
    Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
     
    Ken Bloom, Mar 17, 2009
    #8
  9. Arun Kumar

    Ken Bloom Guest

    On Mon, 16 Mar 2009 19:42:00 -0500, Ken Bloom wrote:

    > 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.


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

    --Ken

    --
    Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
     
    Ken Bloom, Mar 18, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DGW

    Waveform Interpreted

    DGW, Oct 20, 2003, in forum: VHDL
    Replies:
    0
    Views:
    548
  2. Arpan

    Compiled, Not Interpreted!

    Arpan, Nov 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,618
    Karl Seguin
    Nov 25, 2004
  3. Tim Tyler
    Replies:
    36
    Views:
    1,493
    Darryl L. Pierce
    Dec 10, 2004
  4. Chris  Chiasson
    Replies:
    6
    Views:
    634
    Richard Tobin
    Nov 14, 2006
  5. R.. Kumar 1.9.1 OSX
    Replies:
    11
    Views:
    318
    Josh Cheek
    Jul 1, 2010
Loading...

Share This Page