logo       

RE: Q) about singleton methods and access control: msg#01148

lang.ruby.general

Subject: RE: Q) about singleton methods and access control

Christoph <chr_news@xxxxxxx> wrote in message

>>"Bennett, Patrick" <Patrick.Bennett@xxxxxxxx> 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



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise