2

I am trying to follow the short example in the following answer on using custom functions in rails:

http://stackoverflow.com/questions/2879679/where-to-put-code-snippets-in-rails

In math.rb in lib/math.rb

module Math
    class << self
        def cube_it(num)
          num*3
        end
    end
end

In rails console I have tried

include Math
Math.cube_it(2)

But I get the error:

NoMethodError: undefined method 'cube_it' for Math:module
Zakoff
  • 12,665
  • 5
  • 22
  • 35

1 Answers1

1

check config/application.rb for next line

# Custom directories with classes and modules you want to be autoloadable.
config.autoload_paths += %W(#{config.root}/lib)

So if you have still unloadable extension you can type

require 'math'

and recheck

instead of call require, you can create config/initializers/lib.rb

with

Dir[File.join(Rails.root, "lib", "*.rb")].each {|l| require l }

Fivell
  • 11,829
  • 3
  • 61
  • 99
  • I have added the above line, and also rebooted my linode server however I still get the same error. When I type **include Math** into rails console it returns object, when I type **Math.cube_it(2)** afterwards it gives the same error – Zakoff Jan 29 '12 at 13:05
  • try reload! and then Math.methods.grep /cube/ – Fivell Jan 29 '12 at 13:12
  • it only can mean that your extension still unloaded. Check methods which Math contains from console , as I mentioned above – Fivell Jan 29 '12 at 13:17
  • When I do the above that you suggested **try reload! and then Math.methods.grep /cube/** it returns **[]** – Zakoff Jan 29 '12 at 13:20
  • strange... It just means that your extenstion still unloaded – Fivell Jan 29 '12 at 13:26