tags:

views:

98

answers:

1

What's the difference between require 'cool_lib' and gem 'cool_lib'?

+3  A: 

gem gives you more options as to which exact gem you load: see here for plenty of details. In this example, there's no effective difference.

I'd recommend using require unless you specifically need the power of gem - it seems to be what most people use.

Peter
Thank you very much for your explanation. I really appreciate it.
TK
One difference is that if you are sharing your code with someone who doesn't use rubygems, the 'require' form will still work for them (as long as they have manually installed cool_lib in the include path).
invariant
Good answer, but don't "require 'rubygems'". See: http://www.rubyinside.com/why-using-require-rubygems-is-wrong-1478.html .
Wayne Conrad
@Wayne: I've removed the `require 'rubygems'`; fair point. I mainly said it since it's an easy 'gotcha' for new players.
Peter
@Peter, Fair point. It was my gotcha until just recently.
Wayne Conrad
Maybe this should be a separate question, but if `require 'rubygems'` is wrong, is it wrong to use `gem`?
Andrew Grimm