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
0

1 Answer 1

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 }

Sign up to request clarification or add additional context in comments.

5 Comments

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
try reload! and then Math.methods.grep /cube/
it only can mean that your extension still unloaded. Check methods which Math contains from console , as I mentioned above
When I do the above that you suggested try reload! and then Math.methods.grep /cube/ it returns []
strange... It just means that your extenstion still unloaded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.