Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I encapsulate included module methods in Ruby?
    primarykey
    data
    text
    <p>I want to be able to have methods in a module that are not accessible by the class that includes the module. Given the following example:</p> <pre><code>class Foo include Bar def do_stuff common_method_name end end module Bar def do_stuff common_method_name end private def common_method_name #blah blah end end </code></pre> <p>I want Foo.new.do_stuff to blow up because it is trying to access a method that the module is trying to hide from it. In the code above, though, Foo.new.do_stuff will work fine :(</p> <p>Is there a way to achieve what I want to do in Ruby?</p> <p>UPDATE - The real code</p> <pre><code>class Place &lt; ActiveRecord::Base include RecursiveTreeQueries belongs_to :parent, {:class_name =&gt; "Place"} has_many :children, {:class_name =&gt; 'Place', :foreign_key =&gt; "parent_id"} end module RecursiveTreeQueries def self_and_descendants model_table = self.class.arel_table temp_table = Arel::Table.new :temp r = Arel::SelectManager.new(self.class.arel_engine).from(model_table).project(model_table.columns).join(temp_table).on('true').where(model_table[:parent_id].eq(temp_table[:id])) nr = Place.scoped.where(:id =&gt; id) q = Arel::SelectManager.new(self.class.arel_engine) as = Arel::Nodes::As.new temp_table, nr.union(r) arel = Arel::SelectManager.new(self.class.arel_engine).with(:recursive,as).from(temp_table).project(temp_table[:id]) self.class.where(model_table[:id].in(arel)) end def self_and_ascendants model_table = self.class.arel_table temp_table = Arel::Table.new :temp r = Arel::SelectManager.new(self.class.arel_engine).from(model_table).project(model_table.columns).join(temp_table).on('true').where(temp_table[:parent_id].eq(model_table[:id])) nr = Place.scoped.where(:id =&gt; id) q = Arel::SelectManager.new(self.class.arel_engine) as = Arel::Nodes::As.new temp_table, nr.union(r) arel = Arel::SelectManager.new(self.class.arel_engine).with(:recursive,as).from(temp_table).project(temp_table[:id]) self.class.where(model_table[:id].in(arel)) end end </code></pre> <p>Clearly this code is hacked out and due some serious refactoring, and the purpose of my question is to find out if there is a way I can refactor this module with impunity from accidentally overwriting some method on ActiveRecord::Base or any other module included in Place.rb.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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