Note that there are some explanatory texts on larger screens.

plurals
  1. POrails 3.2.1 custom module restrict private instance methods access by the controller
    text
    copied!<p>** Using Rails :3.2.1, Ruby: ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux] **</p> <p>In my module I have one private instance method (get_tables_of_random_words) and one module function (get_random_word) .</p> <p>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.</p> <p>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.</p> <p><strong>Util::RandomWordsUtil</strong></p> <pre><code> 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 </code></pre> <p><strong>GamesController</strong> (Scaffold generate controller- customized)</p> <pre><code> class GamesController &lt; 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 </code></pre> <p>Following are the approaches I tried but didn't worked as desired. Reference: <a href="https://stackoverflow.com/questions/318850/private-module-methods-in-ruby">Private module methods in Ruby</a></p> <p><strong>Util::RandomWordsUtil</strong> (Tried Approach-1) # get_tables_of_random_words could not be found error is prompted from get_random_word method</p> <pre><code> module Util module RandomWordsUtil def self.included(base) class &lt;&lt; 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 </code></pre> <p><strong>Util::RandomWordsUtil</strong> (Tried Approach-2) # Error is prompted from the controller saying undefined local variable or method 'get_random_word' </p> <pre><code> module Util module RandomWordsUtil def self.included(base) class &lt;&lt; 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 </code></pre> <p>Thanks,<br> Jignesh</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload