0

** Using Rails :3.2.1, Ruby: ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux] **

In my module I have one private instance method (get_tables_of_random_words) and one module function (get_random_word) .

From my Rails Controller I am invoking the module function and it works without any issues. However when I am invoking the private instance method of module that too gets invoked without any problem.

Can anybody please explain the reason behind such behavior and how to achieve the functionality I desire.I do not desire to get my module's private instance methods invoked from the class which includes my module.My private instance method is a utility method which is needed to work from inside of module only.

Util::RandomWordsUtil

    module Util
        module RandomWordsUtil
            def get_tables_of_random_words
                # Implementation here
            end

            private :get_tables_of_random_words 

            module_function

            def get_random_word
                # invoke get_tables_of_random_words
            end

        end

    end

GamesController (Scaffold generate controller- customized)

        class GamesController < ApplicationController

          include Util::RandomWordsUtil

          # GET /games
          # GET /games.json
          def index
          end

          def play
            @game = Game.find(params[:id])

            @random_word = get_random_word # This is a module_function
            @random_table = get_tables_of_random_words # This I have marked as private in my module still it gets invoked!

            # Render action show
            render "show"
          end

          # GET /games/1
          # GET /games/1.json
          def show
          end

          # GET /games/new
          # GET /games/new.json
          def new
          end

          # GET /games/1/edit
          def edit
          end

          # POST /games
          # POST /games.json
          def create
          end

          # PUT /games/1
          # PUT /games/1.json
          def update
          end

          # DELETE /games/1
          # DELETE /games/1.json
          def destroy
          end
        end

Following are the approaches I tried but didn't worked as desired. Reference: Private module methods in Ruby

Util::RandomWordsUtil (Tried Approach-1) # get_tables_of_random_words could not be found error is prompted from get_random_word method

    module Util
        module RandomWordsUtil
            def self.included(base)
                class << base
                    def get_tables_of_random_words
                        # Implementation here
                    end

                    private :get_tables_of_random_words 
                end
            end

            module_function

            def get_random_word
                # invoke get_tables_of_random_words
            end

        end

    end

Util::RandomWordsUtil (Tried Approach-2) # Error is prompted from the controller saying undefined local variable or method 'get_random_word'

    module Util
        module RandomWordsUtil
            def self.included(base)
                class << base
                    def get_random_word
                        # invoke get_tables_of_random_words 
                    end

                    private 
                    def get_tables_of_random_words
                        # Implementation here
                    end

                end
            end
        end
    end

Thanks,
Jignesh

Community
  • 1
  • 1
Jignesh Gohel
  • 6,236
  • 6
  • 53
  • 89

1 Answers1

0

I think that when you "include" the module, you are making it a mixin so that all the methods in it become methods of the class including it. Have you tried "require"ing it instead?

Carilda
  • 161
  • 2
  • 12
  • Yes I did try requiring the module but I face the error that No such file to load.May be I didn't specified the path to the file correctly.In a Rails App my utility modules lies at path lib/util/.I could not figure out What path should be used by require method in controller to use the modules? At http://stackoverflow.com/questions/3356742/best-way-to-load-module-class-from-lib-folder-in-rails-3 a comment by ben_h to Mike Fischer says require should not be used within a rails app. – Jignesh Gohel Mar 02 '12 at 07:33
  • I traced down the links and found: config.autoload_paths += Dir["#{config.root}/lib/**/"] # include all subdirectories -- does that work for you? – Carilda Mar 06 '12 at 01:39
  • Yes following is the current setting in my config/application.rb :config.autoload_paths += Dir["#{config.root}/lib/**/"] and it is working. – Jignesh Gohel Mar 07 '12 at 10:58