Rails: I can't call a function in a module in /lib - what am I doing wrong?

前端 未结 9 1751
鱼传尺愫
鱼传尺愫 2021-01-31 08:08

I know I\'m doing something stupid or failing to do something intelligent - I\'m frequently guilty of both.

Here\'s an example of what\'s causing me pain:

I have

相关标签:
9条回答
  • 2021-01-31 08:11

    Today you can do it using module_function notation.

    module TestFunctions
      def abc
        puts 123
      end
    end
    

    Now TestFunctions.abc prints "123"

    A little more about module_function: https://apidock.com/ruby/Module/module_function

    0 讨论(0)
  • 2021-01-31 08:13

    Try this code:

    service = Class.new { extend TestFunctions }
    service.method_four
    
    0 讨论(0)
  • 2021-01-31 08:20

    You can also use module_function like so:

    module TestFunctions
      def abc
        puts 123
      end
    
      module_function :abc
    end
    
    TestFunctions.abc  # => 123
    

    Now you can include TestFunctions in class and call "abc" from within TestFunctions module.

    0 讨论(0)
  • 2021-01-31 08:20

    I messed with this for a while and learned several things. Hopefully this will help someone else out. I am running Rails 3.2.8.

    My module (utilities.rb) looks like this and is in the /lib directory of my rails app:

    module Utilities
    
      def compute_hello(input_string)
         return "Hello #{input_string}"
      end
    
    end
    

    My test (my_test.rb) looks like this and is in the /test/unit directory of my rails app:

    require "test_helper"
    require "utilities"
    
    class MyTest < ActiveSupport::TestCase
      include Utilities
    
      def test_compute_hello
        x = compute_hello(input_string="Miles")
        print x
        assert x=="Hello Miles", "Incorrect Response"
      end
    
    end
    

    Here are a few things to note: My test extends ActiveSupport::TestCase. This is important because ActiveSupport adds /lib to the $LOAD_PATH. (seehttp://stackoverflow.com/questions/1073076/rails-lib-modules-and)

    Secondly, I needed to both "require" my module file, and also "include" the module. Lastly, it is important to note that the stuff that gets included from the module essentially gets placed in the test class. So... be careful that the module that you include doesn't start with "test_". Otherwise, Rails will attempt to run your module method as a test.

    0 讨论(0)
  • 2021-01-31 08:23

    You need to prefix your function with the module name because modules are not classes:

    Your /lib/test_functions.rb:

    module TestFunctions
      def TestFunctions.abc
        puts 123
      end
    end
    

    Your code using the module method:

    require 'test_functions'
    TestFunctions.abc
    
    0 讨论(0)
  • 2021-01-31 08:24

    You can't call a method in a Module directly. You need to include it in a class. Try this:

    >> class MyTest
    >>   include TestFunctions
    >> end
    => MyTest
    >> MyTest.new.abc
    123
    => nil
    
    0 讨论(0)
提交回复
热议问题