Robert Dober
8/23/2007 9:11:00 PM
On 8/23/07, Zeekar <markjreed@gmail.com> 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, 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.
>
> Doable?
Sure, but clumsy, I do not know if it is worth it
class Foo
class << self
private
def x; 60 end
end
attr_reader :x
helper = method(:x)
define_method :initialize do @x = helper.call end
end
puts Foo.new.x
HTH
Robert
>
>
>
--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn