why 1.hour doesn't work?

K

Kenneth

this is a newbie question:

so if it is in Ruby on Rails, then 1.hour and 1.hour.from_now both
work....
now then, how come in irb, they both don't work?
 
S

Summercoolness

Because they're defined in a Rails library, not in the Ruby language.

So is there a way to have all that's in Rails also available in Ruby?
(or at least for 1.hour and 1.hour.from_now )
 
P

Pierre-Charles David

2007/7/2 said:
this is a newbie question:

so if it is in Ruby on Rails, then 1.hour and 1.hour.from_now both
work....
now then, how come in irb, they both don't work?

They're not part of the core language or even the standard library,
but added by Rails. Ruby's classes are open, so libraries can add new
methods even to core classes (in this cas Numeric).

If you want to uses these methods outside of Rails, you need to load
the part of Rails which contributes them (in this case ActiveSupport):

% irb
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'active_support'
=> true
irb(main):003:0> 1.hour
=> 3600
 
K

Kenneth Ll

% irb
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'active_support'
=> true
irb(main):003:0> 1.hour
=> 3600

both are needed huh? is there a way just to include one module? (such
as when including 1 and it detects it lacks the other one, then it will
include the other one too).

so i think the other way

ruby script/console

will work too huh? (my ruby was set up with rails last time but right
now it doesn't have the PATH to do things any more...)
 
T

Tim Hunter

Kenneth said:
both are needed huh? is there a way just to include one module? (such
as when including 1 and it detects it lacks the other one, then it will
include the other one too).

so i think the other way

ruby script/console

will work too huh? (my ruby was set up with rails last time but right
now it doesn't have the PATH to do things any more...)
Typically you do something external to Ruby to ensure that rubygems is
always loaded. Then you can stop worrying about it. The easiest thing to
do is set the RUBYOPTS environment variable to the value "rubygems". How
you set the variable depends on the shell you're using. If you're using
bash, for example, do this:

export RUBYOPTS=rubygems

If you're using another shell there will be a slightly different command.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,981
Latest member
satome

Latest Threads

Top