0

I am trying to write a simple script for my Rails 2.3 application, but I am encountering a problem.

When I start rails console and type Time.now - 1.month it gives the correct output:

>> Time.now - 1.month
=> Mon Dec 07 17:05:50 +0100 2015

When I use the same piece of code inside a file (script/foo.rb) then I get the error

undefined method 'month' for 1:Fixnum (NoMethodError)

I am also not able to require files from the "lib" directory. In Rails 2.3 the "lib" directory is already inside $LOAD_PATH. This problem has given me a headache. Hopefully someone can help me.

Here are details about my system:

$ which -a ruby
/usr/bin/ruby
/Users/rakesh/.rvm/rubies/ruby-1.8.7-p374/bin/ruby

The same script works fine on my friend's machine so I am guessing that something is wrong in my computer.

r3b00t
  • 6,953
  • 6
  • 27
  • 37
  • 1
    This method (as many other) is added but Rails. You can't use it outside of Rails. – Mike Szyndel Jan 07 '16 at 16:25
  • You should load `Active Support` to be able to use these extensions: `require 'active_support/all'` – markets Jan 07 '16 at 16:27
  • When code works on one machine and not on another then you have narrowed the problem significantly. If you both run the same version of Ruby and Rails, then the problem isn't in Rails or Ruby, it's in your configuration or code. Your question is premature; You need to do more research. See "[ask]", "[mcve]" and http://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – the Tin Man Jan 07 '16 at 16:28
  • @ markets I am using it inside Rails 2.3. Rails automatically loads Active Support. – r3b00t Jan 07 '16 at 16:28
  • @MichalSzyndel I am not using it outside Rails. I am trying to use it in Rails project. – r3b00t Jan 07 '16 at 16:29
  • I don't understand why someone is downvoting this question. – r3b00t Jan 07 '16 at 16:29
  • See my comment; I didn't down vote you, but your question doesn't show effort or enough information for us to do anything but answer with broad answers, and that's not what SO is for. – the Tin Man Jan 07 '16 at 16:30
  • You should execute that script with `rails r script/foo.rb`. I guess you're using ruby interpreter directly and because of that you receive that error. – Mareq Jan 07 '16 at 16:31
  • @theTinMan what other information do you think is required for this question. – r3b00t Jan 07 '16 at 16:31
  • @theTinMan yes you are correct, I was using ruby interpreter directly. let me try yr approach. – r3b00t Jan 07 '16 at 16:32
  • Please do not write titles like "Rails ... not working." If you can work the tags into the sentence naturally that's OK but arbitrarily sticking them at the start or end only makes an awkward sentence. Instead create a short and definitive sentence that clearly describes the problem. – the Tin Man Jan 07 '16 at 16:33
  • @theTinMan because as soon as you delete Rails, ppl think I am trying to use Time.now - 1.month for Ruby script, which is not correct. Lets now worry about title. – r3b00t Jan 07 '16 at 16:34
  • 1
    If you want Rails to load the whole environment in custom scripts (scripts/* or whatever), you should use `rails runner`. If you just need Active Support, try to explicitly require it. – markets Jan 07 '16 at 16:36
  • @theTinMan `rails r script/foo.rb` is not working for Rails 2.3. It created new rails app inside my existing app. – r3b00t Jan 07 '16 at 16:36
  • @markets can i user `rails runner` with Rails 2.3 ? – r3b00t Jan 07 '16 at 16:37
  • 1
    For Rails 2.X I think it should be: `script/runner` – markets Jan 07 '16 at 16:38
  • @markets I will give that a try. – r3b00t Jan 07 '16 at 16:38
  • 1
    @markets you are legend. `./script/runner /` Works fine and thanks for explaining it. please write your answer below so that I can mark it as accepted answer. Thanks alot. – r3b00t Jan 07 '16 at 16:39

2 Answers2

7

1.month comes from Active Support, not Ruby stdlib, so you need to load this dependency into your script context.

So, if you want to run this custom script under the whole Rails environment (with all dependencies loaded), you should use rails runner:

runner runs Ruby code in the context of Rails non-interactively

$ bin/rails runner path_to_your_script.rb

In Rails 2: script/runner.

Alternatively, if you just need to use Active Support methods, try to explicitly require it: require 'active_support/core_ext/integer/time' and you'll be able to run the script without loading Rails fully (no need to use the runner).

markets
  • 6,709
  • 1
  • 38
  • 48
  • 1
    Don't use `require 'active_support/all'` unless *ALL* of Active Support is needed. Instead use Active Support's core extensions. See http://stackoverflow.com/a/4239625/128421 – the Tin Man Jan 07 '16 at 18:28
0

The month method has been added to Integer class by Rails Active Support Core extensions.

You need to require the relevant file if you wish to use that method in a stand-alone Ruby script.

require "active_support/core_ext/integer/time"
p Time.now - 1.month
#=> 2015-12-07 22:21:13 +0530

You could also load all the extensions by using

require "active_support/all"
Wand Maker
  • 18,476
  • 8
  • 53
  • 87