Q) about singleton methods and access control

Christoph chr_news@gmx.net wrote in message

“Bennett, Patrick” Patrick.Bennett@inin.com wrote in message
This is what I’d “like” to do:
class A

def initialize()
@varInternalOnly = ‘test’
end

def A.createSpecialA()
testObj = A.new
testObj.internalHelperMethod()

internalHelperMethod is an ``instance method’’ not a Class method
(see below).

Maybe I’m missing something here, but… I called it from an instance.
You saw the ‘testObj = A.new’ - right? ;>

Anyway, as the short term fix you can
simply write

   testObj.send(:internalHelperMethod)

That works(!)… but why? (scratching head)
Why does send bypass access control?

Note that C++ static (class) method are quite different to Ruby’s class methods

I never expected them to be the same - I was just curious why a fairly ‘obvious’ (at least to me) situation like being
able to have a static method create instances of the classes it’s defined in and access private methods wasn’t supported in Ruby.

Cheers,
Patrick Bennett

“Bennett, Patrick” wrote

Maybe I’m missing something here, but… I called it from an instance.
You saw the ‘testObj = A.new’ - right?

No, the call is on the ``class Level’’ - for example I get

···

in class_meth self = A with self.type = Class
in class_meth self is still = A with self.type = Class
in an_instance_method self = #<A:0x2789ad8> with self.type = A

when running


class A
def A.class_meth
puts “in class_meth self = #{self} with self.type = #{type}”
obj = A.new
puts “in class_meth self is still = #{self} with self.type =
#{type}”
end

versus

def an_instance_method
puts “in an_instance_method self = #{self} with self.type =
#{type}”
end
end

A.class_meth
inst = A.new
inst.an_instance_method

That works(!)… but why? (scratching head)
Why does send bypass access control?

Well it does, so just use it …

/Christoph

Christoph wrote:

“Bennett, Patrick” wrote

Maybe I’m missing something here, but… I called it from an instance.
You saw the ‘testObj = A.new’ - right?

No, the call is on the ``class Level’’ - for example I get

No it’s not. I was calling testObj.xxxxx - that’s definitely an
“instance” level call. :
Arrgggh…


in class_meth self = A with self.type = Class
in class_meth self is still = A with self.type = Class
in an_instance_method self = #<A:0x2789ad8> with self.type = A

when running


class A
def A.class_meth
puts “in class_meth self = #{self} with self.type = #{type}”
obj = A.new
puts “in class_meth self is still = #{self} with self.type = #{type}”
end

This example makes no sense to me at all. What exactly are you testing?
Why would you think ‘self’ would change in the middle of a method?
Why aren’t you calling an instance method of obj (that you created in
the singleton method)? That’s the whole point of this issue. The fact that
singleton’s can’t create an >instance< and call that >instance’s<
private >instance< method (as in C++) - since the singleton is
technically a member of the class, and should
be able to workin within its security constraints. Apparently, Ruby
just doesn’t support it. Fine, I can live with it. I’m still curious
as to ‘why’ though - since it’s seems
to be an odd ommision.

versus

def an_instance_method
puts “in an_instance_method self = #{self} with self.type = #{type}”
end
end

A.class_meth
inst = A.new
inst.an_instance_method

That works(!)… but why? (scratching head)
Why does send bypass access control?

Well it does, so just use it …

I will… thanks. :>
(even though it seems a strange hack)

Patrick Bennett wrote:

Christoph wrote:

No, the call is on the ``class Level’’ - for example I get

No it’s not. I was calling testObj.xxxxx - that’s definitely an
“instance” level call. :
Arrgggh…

You are missing the point. When you call testObj.xxxx, yes you are
calling a instance method. However, you are not making an instance
level call. What is meant by “instance level” is the context the call is
made from. And the context is a class singleton method, not an instance
method.

HTH

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)