ruby - Defining a method visible only within a certain module/class -


define a method foo on module / class A Is there a way so that it will only appear within the module / class B , or its lineage? The following shows this situation:

  ANU.FU # => Undefined class b. ANU.fu # = & gt; Defined def bar ANUFU # = & gt; Defined enddef self.baz.new.foo # = & gt; Defined end-end class C & lt; B ANUFU # = & gt; Defined def bar ANUFU # = & gt; Defined End Diffself.baz A.new.foo # = & gt; Defined End End I think that I feel easily that the soul was reformed, but it does not seem like what I want.   

this works ^ _ ^

  class is a private def Foo "variable !!!" End and Class B & LT; A public: foo def initialize @foo = self.foo puts the end end "A.new.foo # {A.new.foo Rescue" ... Sorry, no. '} "= & Gt; ANUFu ... sorry, does not put" B.new.foo # {B.new.foo Rescue ... ... sorry, no. "} "= & Gt; BNEU FU !!!   

If you still want to use ANUFU in all subclasses using A class name then you should use the following.

  class is a private def foo "variable !!!" End and Class B. Class A and LT; A public: foo end attr_reader: c ,: D Df c.ne.fu end DEF DNE.U.FU puts at the end of the end "ANU.FU # {ANU.FU Rescue" ... Sorry, no. '} "= & Gt; ANU.FOO ... sorry, does not put B. new.c = & gt; Food !!! puts.new.d => Food !!!    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -