Hi --

On Fri, 24 Aug 2007, Zeekar wrote:

> Found a lot of discussion of this, but it didn't seem to suggest a
> solution to a common pattern.  The below completely useless example is
> modeled after a longer and more useful one in the Agile Web
> Development with Rails book (2nd ed):
>
> class Foo
>    class << self
>          private
>          def staticHelperFunction
>               12345
>          end
>    end
>    public
>    def initialize
>         @var = self.class.staticHelperFunction
>    end
> end
> Foo.new.var
>
> Now, the above won't work, obviously.  If I replace the direct call to
> staticHelperFunction with instance_eval or send, it will work,

Not until you write a 'var' instance method :-)

> but that feels hackish.   Is there an idiomatic Ruby way to handle
> this sort of situation?
>
> In case the goal isn't clear, I want instance (not singleton/class)
> methods of a given class to have access to what are essentially helper
> functions/subroutines (not other instance methods of the same class)
> which are not generally accessible outside of that class.

It seems like you're threading a very narrow needle. Do you mean that
you would not want class methods to see each other?

   class C
     def self.x    # assume this is the kind of method you mean
     end

     def self.y
       x           # This would fail?
     end
   end

That's a pretty tall order: having methods from which you can't call
other singleton methods of the very same object. Or am I
misunderstanding?


David

-- 
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)