문제

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

For example, what's the difference betweenrequire 'minitest' and gem 'minitest'?

도움이 되었습니까?

해결책

Say you have two versions of the gem foo installed:

$ gem list foo

*** LOCAL GEMS ***

foo (2.0.1, 2.0.0)

If you use only require, the newest version will be loaded by default:

require 'foo'       # => true

Foo::VERSION        # => "2.0.1"

If you use gem before calling require, you can specify a different version to use:

gem 'foo', '2.0.0'  # => true
require 'foo'       # => true

Foo::VERSION        # => "2.0.0"

Note: using gem without subsequently calling require does not load the gem.

gem 'foo'           # => true

Foo::VERSION        # => NameError: uninitialized constant Foo

다른 팁

Kernel#require activates the latest version of a gem.

Kernel#gem (added by RubyGems) allows activation of specific gem versions.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top