** 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