Forum: Ruby gem 'mechanize' in Ruby script

Posted by Christopher Frost (christophrost)
on 2013-03-11 11:29
Hi,

I would like to know exactly how this snippets work in Ruby script.

gem 'mechanize', '=1.0.0'
require File.dirname(__FILE__) + "/logo.rb"
require "rubygems"
require "cashew"

I can guess the lines (2~4) will require external ruby files.
But the first line that is using "gem" is horrible thing to me.

When this ruby file gets executed, 'mechanize' gem would be installed?
Or you will have to install that gem beforehand?

I would very appreciate if you can explain further details.

Thanks in advance,
Christopher
Posted by Hans Mackowiak (hanmac)
on 2013-03-11 11:36
information: you should require "rubygems" first before you call the gem 
method if you want to use ruby1.8

on newer ruby, require "rubygems" is mostly useless
Posted by Bartosz Dziewoński (matmarex)
on 2013-03-11 12:32
(Received via mailing list)
On Mon, 11 Mar 2013 11:29:43 +0100, Christopher Frost 
<lists@ruby-forum.com> wrote:

> gem 'mechanize', '=1.0.0'

> When this ruby file gets executed, 'mechanize' gem would be installed?
> Or you will have to install that gem beforehand?

It won't be installed, and it won't even be `require`-d. This line just 
checks if you have the gem installed in that version, and if you don't, 
it raises an exception.

This is also useful when you have multiple gem versions installed 
locally, and want to use a particular (and not latest) one – any 
subsequent call to `require 'mechanize'` or similar will load Mechanize 
in that precise version.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.