Forum: Ruby mechanize & field name for search passed as string

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.
Edouard D. (Guest)
on 2009-01-21 06:48
Hi,

I am writing a method to perform a similar to the below mentionned
google search but i want it to accept the field's name for the search
entry as a string.

In the following example from the Mechanize docs I would expect it to be
search.send('q') instead of search.q but I get this error

./scrapingGoogle.rb:14: syntax error, unexpected '=', expecting kEND
      search.send('q') = 'Hello world'.

Thanks for your comments

********************************************************************

a = WWW::Mechanize.new { |agent|
    agent.user_agent_alias = 'Mac Safari'
  }

  a.get('http://google.com/') do |page|
    search_result = page.form_with(:name => 'f') do |search|
      search.q = 'Hello world'
    end.submit

    search_result.links.each do |link|
      puts link.text
    end
  end
Sandor Szücs (Guest)
on 2009-01-22 12:15
(Received via mailing list)
On 21.01.2009, at 05:46, Edouard D. wrote:
>
> I am writing a method to perform a similar to the below mentionned
> google search but i want it to accept the field's name for the search
> entry as a string.
>
> In the following example from the Mechanize docs I would expect it
> to be
> search.send('q') instead of search.q but I get this error

In the example the method which is called is not q(). It's q=(), so
if you want so send() then use the right method.

> ./scrapingGoogle.rb:14: syntax error, unexpected '=', expecting kEND
>      search.send('q') = 'Hello world'.

search.send('q=', 'Hello World') or by symbol
search.send(:q=, 'Hello World')

regards, Sandor
Szücs
This topic is locked and can not be replied to.