[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

private inner class

John-Mason P. Shackelford

4/1/2005 8:35:00 PM

In ruby is it possible to have a private innner class? Using private
method doesn't appear to work.

class A
private
class B
def hw
'hello_world'
end
end
end

A::B.new.hw # => 'hello world'



John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
john-mason.shackelford@pearson.com
http://pearsonedmeasu...


4 Answers

Glenn Parker

4/1/2005 8:59:00 PM

0

John-Mason P. Shackelford wrote:
> In ruby is it possible to have a private innner class? Using private
> method doesn't appear to work.
>
> class A
> private
> class B
> def hw
> 'hello_world'
> end
> end
> end
>
> A::B.new.hw # => 'hello world'

p A::B.new.hw => "hello world"

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...


gabriele renzi

4/1/2005 9:53:00 PM

0

Glenn Parker ha scritto:
> John-Mason P. Shackelford wrote:
>
>> In ruby is it possible to have a private innner class? Using private
>> method doesn't appear to work.
>>
>> class A
>> private
>> class B
>> def hw
>> 'hello_world'
>> end
>> end
>> end
>>
>> A::B.new.hw # => 'hello world'
>
>
> p A::B.new.hw => "hello world"
>

I guess he meant this is not private.
Anyway, that would be a "yes and no". You can't do that way since
constants are public, but you can use a class variable, wich is private:

>> class External
>> @@private_class = Class.new do
?> def foo
>> 'yuk'
>> end
>> end
>> def gimme_an_object
>> @@private_class.new
>> end
>> end
=> nil
>> External.new.gimme_an_object.foo()
=> "yuk"

Robert Klemme

4/1/2005 10:14:00 PM

0


"Glenn Parker" <glenn.parker@comcast.net> schrieb im Newsbeitrag
news:424DB5F9.6000808@comcast.net...
> John-Mason P. Shackelford wrote:
>> In ruby is it possible to have a private innner class? Using private
>> method doesn't appear to work.
>>
>> class A
>> private
>> class B
>> def hw
>> 'hello_world'
>> end
>> end
>> end
>>
>> A::B.new.hw # => 'hello world'
>
> p A::B.new.hw => "hello world"

?

robert

Glenn Parker

4/2/2005 2:51:00 PM

0

Robert Klemme wrote:
>
> ?

Sorry, I completely misunderstood John-Mason's question. I thought he
wanted A::B.new.hw to work, but that maybe he was confused because
nothing was being printed. Now I understand that he actually wanted it
to fail!

--
Glenn Parker | glenn.parker-AT-comcast.net | <http://www.tetrafoi...