Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between gem and require (require open-uri)

Tags:

ruby

gemfile

I just wanted to understand for my self.

I'm using the nokogiri gem (for parsing HTML). If I got it right to open URLs I need to use a method from the gem 'open-uri'.

But when I include it in my Gemfile (on Windows developer's machine):

gem 'open-uri' - there is an error while bundle install that it can not find gem.

So If I use require 'open-uri' - its working.

So can some explain what is going on?

like image 818
WHITECOLOR Avatar asked Dec 03 '11 10:12

WHITECOLOR


2 Answers

You're using bundler for your gem dependecies and you're doing it right but OpenUri is part of the Ruby standard library. That's why you only need to require it if you want to use it in your code.

like image 74
lucapette Avatar answered Nov 13 '22 10:11

lucapette


require is used to load another file and execute all its statements. This serves to import all class and method definitions in the file. require also keeps track of which files have been previously required so it doesn't execute it twice.

A RubyGem is a software package, commonly called a “gem”. Gems contain a packaged Ruby application or library. The RubyGems software itself allows you to easily download, install, and manipulate gems on your system. - What is a Gem?:

The Gemfile is then used by bundler to install the specified gems.

open-uri is not a gem but part of the Ruby Standard Library so it just needs to be required.

like image 39
jlundqvist Avatar answered Nov 13 '22 11:11

jlundqvist