Z
Zeekar
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?
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?