Package protected methods

Hi,

is there a possibility to gain a visibility like java’s
package protection construct:
classes in the same module (or class) can access methods,
that are not visible from otside the package.

Eg:
module A
class B
protected
def foo
end
end

class C
def foo
B.new.foo
end
end
end

I don’t want to use the send method, so the only solution at
the moment is simply to not use the protected modifier.
The problem seems to me so common, that there must be a
solution I did not realized - but which?

Best regads,
Matthias

···

A::C.new.foo #no error
A::B.new.foo #raise error


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

Hi,

irb(main):001:0> class Aclass
irb(main):002:1> protected
irb(main):003:1> def faclass1
irb(main):004:2> puts "faclass1"
irb(main):005:2> end
irb(main):006:1> public
irb(main):007:1> def faclass2
irb(main):008:2> puts "faclass2"
irb(main):009:2> end
irb(main):010:1> end
nil
irb(main):011:0> inst = Aclass.new
#Aclass:0x2ad7cd0
irb(main):012:0> inst.faclass2
faclass2
nil
irb(main):013:0> inst.faclass1
NameError: protected method `faclass1’ called for #Aclass:0x2ad7cd0
from (irb):13

regards,
alia

Matthias Veit wrote:

···

Hi,

is there a possibility to gain a visibility like java’s
package protection construct:
classes in the same module (or class) can access methods,
that are not visible from otside the package.

Eg:
module A
class B
protected
def foo
end
end

class C
def foo
B.new.foo
end
end
end

A::C.new.foo #no error
A::B.new.foo #raise error

I don’t want to use the send method, so the only solution at
the moment is simply to not use the protected modifier.
The problem seems to me so common, that there must be a
solution I did not realized - but which?

Best regads,
Matthias


Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

“Matthias Veit” matthias_veit@yahoo.de wrote in message
news:20020702110250.5019a71b.matthias_veit@yahoo.de

is there a possibility to gain a visibility like java’s
package protection construct:
classes in the same module (or class) can access methods,
that are not visible from outside the package.

A timely question :wink: I’m looking for something like this too. There are
certain methods that should only be visible to other classes in the same
module (package) but private outside of that package.

“Lyle Johnson” jlj@cfdrc.com writes:

“Matthias Veit” matthias_veit@yahoo.de wrote in message
news:20020702110250.5019a71b.matthias_veit@yahoo.de

is there a possibility to gain a visibility like java’s
package protection construct:
classes in the same module (or class) can access methods,
that are not visible from outside the package.

A timely question :wink: I’m looking for something like this too. There are
certain methods that should only be visible to other classes in the same
module (package) but private outside of that package.

Even better would be “friend on steroids”, so I could say

friend “db/", "Test

which would give me package level protection and also the ability to
have test classes poke around inside my classes.

Dave

I’m hoping that something like Java’s package protected construct
doesn’t ever get added to Ruby. I really don’t understand the
motivation behind it and I’d love someone to explain it to me.

It just seems like a way for programmers to try and predict what other
programmers who use their code shouldn’t do. It prevents others from
shooting themselves in the foot. We often predict incorrectly and
then other programmers are prevented from overriding the behavior they
need to override. We’re not omniscient. People often use our code in
ways we could have never predicted.

One of the things I like about Ruby is that the language seems to
allow programmers to do what they need to do without having the
constructs which prevent programmers from getting their job done. It
seems that when a language offers handcuffs, programmers use them
liberally and then other programmers constantly feel shackled.

When programming in Java, it seems I’m often thinking things like,
"Just let me override that method! Why are they preventing me from
doing that? I REALLY understand this code and I’m aware of fact that
this method is not normally overridden but this case is different."
Or, “Why can I not have some code before the super call in a
constructor?” Or, “Why did they make that variable private? I wish I
could get to that variable!”

I’ve never experienced this kind of frustration with Ruby. It’s an
incredible language. For the past few weeks, I’ve been spending half
of my day writing Ruby and half writing Java. It has been remarkable
how much less code is needed in Ruby. I’ve been amazed.

I’m definitely not an expert on Ruby scoping or access modifiers so
I’m not sure if I really understand the request for the feature. I
think I do understand Java’s package protected construct. Things are
probably very different in Ruby and I’d love someone to help me
understand.

···

On Tue, 2 Jul 2002 09:15:54 -0500, “Lyle Johnson” jlj@cfdrc.com wrote:

“Matthias Veit” matthias_veit@yahoo.de wrote in message
news:20020702110250.5019a71b.matthias_veit@yahoo.de

is there a possibility to gain a visibility like java’s
package protection construct:
classes in the same module (or class) can access methods,
that are not visible from outside the package.

A timely question :wink: I’m looking for something like this too. There are
certain methods that should only be visible to other classes in the same
module (package) but private outside of that package.


Duff OMelia
RoleModel Software
342 Raleigh Street
Holly Springs, NC 27540
http://www.rolemodelsoftware.com