ruby mechanize requires full class name - ruby

why do you need the full name for mechanize as so:
#!/usr/bin/ruby -w
require 'rubygems'
require 'pp'
require 'yaml'
require "mechanize"
yml = YAML.load_file 'login.yml'
user = yml["user"]
pword = yml["pword"]
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
when the mechanize example doesn't do that? This is asked on top of a previous question. Code only worked after reading the previous question on this exact topic and adding the full class(?) name. I've seem somewhat similar in Java, but only when it's ambiguous. Here, there's nothing ambigious, there's only the one Mechanize.
Pardon, the actual previous question completely contradicts the above link. The previous question I was referencing is here. To reiterate, two different questions, two different answers. Maybe the API or idiom changed.

What version of Mechanize are you using? Try gem list mechanize.
Using Ruby 1.8.7-p357, 1.9.2-p290, and 1.9.3-p0 and Mechanize 2.1 I am able to instantiate an instance. For instance:
1.8.7 :001 > require 'mechanize'
true
1.8.7 :002 > agent = Mechanize.new
#<Mechanize:0x101baacf0
[...]
and:
1.9.3p0 :001 > require 'mechanize'
true
1.9.3p0 :002 > agent = Mechanize.new
#<Mechanize:0x102988610
[...]
I suspect you are using Mac OS, because you are accessing Ruby at /usr/bin. Ruby is not installed by default on Windows or Linux and wouldn't be at that path normally.
Apple's version of Ruby doesn't include Mechanize, so you added it at some point. Because Apple didn't install it it should be benign to update, so do:
sudo gem update mechanize
Apple does use Ruby for apps on Mac OS, so you have to be aware of that when updating their pre-installed gems.

Related

Use Watir in Shoes (ruby)

I'm trying to do an application in ruby. I want to collect information from the user using some UI interface. Then use this info in my script to fill some form on a web page.
I use Shoes as UI
I use Watir as Browser "manager"
Here a simple sample of what i'm trying to do
Shoes.setup do
gem 'watir'
end
require 'watir'
Shoes.app do
stack do
edit_line do |e|
#url = e.text
end
button("Test"){
browser = Watir::Browser.new
browser.goto #url
#Do some stuff
}
end
end
But then When the application start it's trying to installing watir and freeze because of error:
http://screencast.com/t/XWmeMmPQEBc
The error says that rake requires rubygems >= 1.3.2
You either need to upgrade rubygems or downgrade rake to a version compatible with your current rubygems.
Edit: or specifiy a version of watir that will run with an older rubygems & rake
Shoes has this problem with many native gems, i advise you to try this with green shoes
['rubygems','green_shoes','watir'].each(&method(:require))
Shoes.app do
stack do
Watir::Browser.default = 'ie'
browser = Watir::Browser.new
browser.goto 'http://bit.ly/watir-example'
browser.text_field(:name => 'entry.0.single').set 'Watir'
browser.text_field(:name => 'entry.1.single').set "I come here from Australia. \n The weather is great here."
browser.radio(:value => 'Watir').set
browser.button(:name => 'submit').click
end
end

Why doesn't this code using the ruby-mbox gem parse mbox files?

I installed ruby-mbox by doing gem install ruby-mbox
Running this:
#!/usr/bin/ruby
require 'rubygems'
require 'mbox'
m = IO.read('test.eml')
puts m.size
m = Mbox.new(m)
puts m
produces this exception (at line 7):
/Library/Ruby/Gems/1.8/gems/ruby-mbox-0.0.2/lib/mbox/mbox.rb:45:in `initialize': uninitialized constant Mbox::StringIO (NameError)
I have proved that "m" is assigned a string containing the contents of the file, just before Mbox.new(m) is called.
It looks as though the Mbox::StringIO should have been defined by hasn't been.
What's going wrong here?
Ruby version:
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
(That's the default ruby installed on OS X 10.6.6)
Sorry people, I should have looked harder before posting here...
Fixed it:
Just inserted the line require 'stringio' to give this:
#!/usr/bin/ruby
require 'rubygems'
require 'stringio'
require 'mbox'
m = IO.read('test.eml')
puts m.size
m = Mbox.new(m)
puts m
It looks like stringio is assumed to be loaded - but isn't loaded explicitly by ruby-mbox...
Oddly, the example scripts don't load it either...

How to use Capybara in pure Ruby (without Rails)?

I'm trying to get Capybara running in a simple Ruby script -- i.e. without/outside of Rails. Here's the script:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
include Capybara
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
visit('/')
The problem is that when I run this I get this error:
NameError: uninitialized constant Capybara::Session
at top level in dsl.rb at line 52
method gem_original_require in custom_require.rb at line 36
method require in custom_require.rb at line 36
at top level in capybara_test.rb at line 3
method gem_original_require in custom_require.rb at line 31
method require in custom_require.rb at line 31
at top level in capybara_test.rb at line
What am I doing wrong?
Some more info:
Mac OS X 10.5
ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]
capybara (0.3.9)
Thanks!
Neal
Note: Per the comment from jnicklas I tried this, which matches the new README more closely:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.default_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
include Capybara
def test_google
visit('/')
end
end
Unfortunately, I'm still seeing the same error:
NameError: uninitialized constant Capybara::Session
Thoughts?
Thanks!
Here's something that seems to work for me:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
class Test
include Capybara::DSL
def test_google
visit('/')
end
end
end
t = MyCapybaraTest::Test.new
t.test_google
It goes to show that even incorrect documentation lives forever. The Capybara README used to recommend to include Capybara in the global namespace, this is a really bad idea, and messes up any number of random things. You should include Capybara in your own module or class and use that instead.
Check out the README for current best practices.
Please check this CapybaraRspec101 example and fork it.
It's a small example for acceptance tests on http://www.hi5.com using from scratch:
Capybara
Rspec
Selenium-webdriver
All instructions are in the repo

Curb curb-fu gem installation problem

I've installed the Curb and Curb-fu gem and libcurl on my Ubuntu box.
If I go into irb and run the following
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'curb'
=> true
irb(main):003:0> require 'json'
=> true
irb(main):004:0> require 'curb-fu'
=> true
irb(main):005:0>
So it seems that I have access to all the gems.
But I've created a very simple ruby app that's giving me an error:
#!/usr/bin/ruby
require 'rubygems'
require 'curb'
require 'json'
require 'curb-fu'
response = CurbFu.get('http://slashdot.org')
puts response.body
I get the following error back.
/usr/lib/ruby/gems/1.8/gems/curb-fu-0.4.4/lib/curb-fu/authentication.rb:3: uninitialized constant CurbFu::Authentication::Curl (NameError)
I have a feeling it's something to do with libcurl and have tried several different versions but still no joy.
Can anyone offer any assistance?
Cheers
Togs
I managed to get it to work.
I uninstalled both the curb and curb-fu gem and re-installed them.
I now have curb-fu working.
For future reference for anyone having problems with this.. these are the libcurl bits I installed.
libcurl3
libcurl3-gnutls
libcurl4-openssl-dev

How do I require a specific version of a ruby gem?

Specifically, the ruby-oci8 gem. I have both 1.0.7 and 2.0.4 installed. I want 1.0.7.
I can just require oci8, but I don't get the version I want.
irb(main):001:0> require 'oci8'
=> true
irb(main):002:0> OCI8::VERSION
=> "2.0.4"
I can require using the full path to the file, which works, but is not going to be portable:
irb(main):001:0> require 'C:\Ruby\lib\ruby\gems\1.8\gems\ruby-oci8-1.0.7-x86-mswin32-60\lib\oci8'
=> true
irb(main):002:0> OCI8::VERSION
=> "1.0.7"
I can use the gem command to ask for the version I want, but it doesn't appear to actually load the library:
irb(main):001:0> gem 'ruby-oci8', :lib=>'oci8', :version=>'=1.0.7'
=> true
irb(main):002:0> OCI8::VERSION
NameError: uninitialized constant OCI8
from (irb):2
I would definitely favor this last approach if would load the library, rather than just confirming that it's present on my system. What am I missing?
My problem was twofold:
1) confusing gem command syntax with that used in config.gem lines in a rails environment.rb configuration file.
2) failing to issue a require command after the gem command.
Proper usage in a script is:
gem 'ruby-oci8', '=1.0.7'
require 'oci8' # example is confusing; file required (oci8.rb) is not
# same name as gem, as is frequently the case
Proper usage in a rails 2.3.x environment.rb file is:
config.gem "ruby-oci8", :version=>'1.0.7'
Thanks to the folks at http://www.ruby-forum.com/topic/109100
Try the following syntax (instead of require):
require_gem 'RMagick' , '=1.10'
require_gem 'RMagick' , '>=1.10'
require_gem 'rake', '>=0.7.0', '<0.9.0'

Resources