difference between require and load in ruby
Q. Well the question is very simple. In ruby, what’s the difference between require and load?
Ans: require and load almost does the same thing. The important difference is that require remembers the loaded files and ensures that they are loaded only once.
This begs us to question, when we already have require then why would you need load.
require is good for scripts but load is much better when working with scripts and irb.
eg: Suppose i’m making a script and i want to keep testing the functionality with irb.
[root@server ~]# cat linuxguy.rb
puts "hello ruby"
1.9.3-p125 :003 > load 'linuxguy.rb'
hello ruby
=> true
Now i’ll make some changes to the script and try it again.
[root@server ~]# cat linuxguy.rb
puts "hello ruby"
puts "We love ruby"
1.9.3-p125 :004 > load 'linuxguy.rb'
hello ruby
We love ruby
=> true
copy gems from one gemset to another
I”m using ruby 1.9.3 and i have multiple gemsets.
[root@server tmp]# ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
[root@server tmp]# rvm gemset use new
Using /usr/local/rvm/gems/ruby-1.9.3-p125 with gemset new
[root@server tmp]# rvm gemset list
gemsets for ruby-1.9.3-p125 (found in /usr/local/rvm/gems/ruby-1.9.3-p125)
global
=> ispunity
new
[root@server tmp]# gem list
*** LOCAL GEMS ***
alchemist (0.1.2.1)
builder (3.0.0)
bundler (1.1.3)
bunny (0.7.9)
chef (0.10.8)
diff-lcs (1.1.3)
erubis (2.7.0)
excon (0.12.0)
fog (1.3.0)
formatador (0.2.1)
highline (1.6.11)
i18n (0.6.0)
ipaddress (0.8.0)
json (1.6.1)
knife-kvm (0.1.2)
log4r (1.1.10)
mime-types (1.18)
mixlib-authentication (1.1.4)
mixlib-cli (1.2.2)
mixlib-config (1.1.2)
mixlib-log (1.3.0)
moneta (0.6.0)
multi_json (1.2.0)
net-scp (1.0.4)
net-ssh (2.1.4)
net-ssh-gateway (1.1.0)
net-ssh-multi (1.1)
nokogiri (1.5.2)
ohai (0.6.12)
polyglot (0.3.3)
rake (0.8.7)
rest-client (1.6.7)
rspec (2.8.0)
rspec-core (2.8.0)
rspec-expectations (2.8.0)
rspec-mocks (2.8.0)
ruby-hmac (0.4.0)
systemu (2.5.0)
terminal-table (1.4.5)
treetop (1.4.10)
uuidtools (2.1.2)
yajl-ruby (1.1.0)
[root@server tmp]# rvm gemset use new
Using /usr/local/rvm/gems/ruby-1.9.3-p125 with gemset new
[root@server tmp]# gem list
*** LOCAL GEMS ***
What i want is to have all the gems in the gemset ispunity available in gemset new. But i don’t want to install all these gems all over again. Guess what, you don’t have to.
You can copy the gems from one gemset to another gemset. And it’s really simple.
[root@server tmp]# rvm gemset copy 1.9.3@ispunity 1.9.3@new
Copying gemset from 1.9.3@ispunity to 1.9.3@new
Making gemset for 1.9.3@new pristine.
[root@server tmp]# rvm gemset list
gemsets for ruby-1.9.3-p125 (found in /usr/local/rvm/gems/ruby-1.9.3-p125)
global
ispunity
=> new
[root@server tmp]# gem list
*** LOCAL GEMS ***
alchemist (0.1.2.1)
builder (3.0.0)
bundler (1.1.3)
bunny (0.7.9)
chef (0.10.8)
diff-lcs (1.1.3)
erubis (2.7.0)
excon (0.12.0)
fog (1.3.0)
formatador (0.2.1)
highline (1.6.11)
i18n (0.6.0)
ipaddress (0.8.0)
json (1.6.1)
knife-kvm (0.1.2)
log4r (1.1.10)
mime-types (1.18)
mixlib-authentication (1.1.4)
mixlib-cli (1.2.2)
mixlib-config (1.1.2)
mixlib-log (1.3.0)
moneta (0.6.0)
multi_json (1.2.0)
net-scp (1.0.4)
net-ssh (2.1.4)
net-ssh-gateway (1.1.0)
net-ssh-multi (1.1)
nokogiri (1.5.2)
ohai (0.6.12)
polyglot (0.3.3)
rest-client (1.6.7)
rspec (2.8.0)
rspec-core (2.8.0)
rspec-expectations (2.8.0)
rspec-mocks (2.8.0)
ruby-hmac (0.4.0)
systemu (2.5.0)
terminal-table (1.4.5)
treetop (1.4.10)
uuidtools (2.1.2)
yajl-ruby (1.1.0)