Skip to content

Commit

Permalink
adding files for Gemfile section
Browse files Browse the repository at this point in the history
  • Loading branch information
mindaslab committed Nov 5, 2014
1 parent 89a0c33 commit 2aea740
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fetch_data/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem 'rest-client'
14 changes: 14 additions & 0 deletions fetch_data/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GEM
remote: https://rubygems.org/
specs:
mime-types (2.4.1)
netrc (0.7.7)
rest-client (1.7.2)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)

PLATFORMS
ruby

DEPENDENCIES
rest-client
78 changes: 78 additions & 0 deletions fetch_data/gem file
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Gem file and its use

You must have heard of Ruby gems, you must have created it and possibly published it on http://rubygems.org . Now lets see whats the use of Gemfile.

Checkout these files:

The first one is called requester.rb

# requester.rb

require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

resource = RestClient::Resource.new 'http://nothing.com'
p resource.get

The second one is the Gemfile

source 'https://rubygems.org'

gem 'rest-client'

Put both in the same folder. If you look at requester.rb, it sends a request to http://nothing.com using the following lines

resource = RestClient::Resource.new 'http://nothing.com'
p resource.get

and prints it.

Fotr this to take place we need install a gen called rest-client using the command

$ gem install rest-client

and we need to require it requester.rb using the following line

require 'rest-client'

In other words the code must look as shown

require 'rest-client'

resource = RestClient::Resource.new 'http://nothing.com'
p resource.get

So why we have these three lines

require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

Instead of one? Well let me explain. First this one is a simple project, which requires just one gem, in reality we might need tens of them in real life project. before running the project if we do not have those gems in our system we need to manulaly check if each and every gem esists and install it. This might be simple for few gems, but the truth is if we have lots of gems we are going to hate it.

Welcome to the Ruby way, here is where the Gemfile comes aas saviour. Lets analyze it. Open up the Gemfile, the first line is

source 'https://rubygems.org'

This tells the bundler 9the thing that fetches and install gems) from where the gems must be fetched. Almost all ruby gems end up in http://rubygems.org. there are some bad guys however who like to have proprietary code and don't release it to public. Those suckers keep it for themselves.

Next we just list the gems needed in gem "<gem-name>" one by one. In this case we just have only one gem and so we list it as

gem 'rest-client'

Next in the ruby program that needs those gems we put this piece of code at the top

require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

I do not know what it does exactly but this requires all gems specified in the Gemfile and thus making available readily all gems we need to run the program. Possibly if I learn more in the future I will update this section or most propably not. All you have to do to fetch and install all the gems into the system is this in the terminal

$ bundle install

or in short

$ bundle

Thats it. All the gems in its latest version will get installed in your system and will be availabe for the program that needs it. Enjoy life!
8 changes: 8 additions & 0 deletions fetch_data/requester.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# requester.rb

require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

resource = RestClient::Resource.new 'http://nothing.com'
p resource.get

0 comments on commit 2aea740

Please sign in to comment.