sjain
sjain

Reputation: 23344

strange behaviour - irb and rails console

The irb is giving true at first and then false always for the command require rails.

The rails console is giving false always.

How's this happening?

Please see below cmd-

~/Workspaces/eclipse/image_cropper_ws/image_cropper$ irb

1.9.2-p180 :001 > require 'rails'
=> true 
1.9.2-p180 :002 > require 'rails'
=> false 
1.9.2-p180 :003 > exit



~/Workspaces/eclipse/image_cropper_ws/image_cropper$ rails console
Loading development environment (Rails 3.2.8)

1.9.2-p180 :001 > require 'rails'
=> false 
1.9.2-p180 :002 > require 'rails'
=> false 

Upvotes: 1

Views: 215

Answers (2)

Nils Landt
Nils Landt

Reputation: 3134

require returns false when what you're trying to require is already loaded - the first time you require 'rails', it's not loaded, and require returns true.
The second time you require 'rails', it is already loaded and require returns false.

Rails is always loaded in the rails console.

Upvotes: 2

Paul.s
Paul.s

Reputation: 38728

Check the docs for require, it states

Loads the given name, returning true if successful and false if the feature is already loaded.

So the first time you call require in irb it loads and returns true. The second time it is already loaded so it returns false.

When you call rails c it loads irb with your rails environment so it must have already required rails

Upvotes: 1

Related Questions