Why 1.hour doesn't work?

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?

Hi –

On Mon, 2 Jul 2007, Kenneth wrote:

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?

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

David

2007/7/2, Kenneth [email protected]:

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

Kenneth Ll wrote:

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.

% 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…)

This is why I always stress that people should learn Ruby first before
moving on to Rails. Rails does so much to the Ruby stdlib that it’s very
difficult to get a grasp on things if you’re new to both.

Please read through this book to get you started on Ruby:

http://www.rubycentral.com/book/

Jason