From: Ian Macdonald Date: 2002-04-26T01:27:26+09:00 Subject: Re: appending methods to class inside module On Thu 25 Apr 2002 at 16:44:57 +0900, Ed Sinjiashvili wrote: > That's right, you define String class under Dog namespace - its fully > qualified name is Dog::String. Ah, OK. I was confused about the namespace issue. > > My question is: how can I get String#bark to be defined, so that I can > > then call the bark method on foo? > Just define it at a toplevel. OK, so now I have something like this: class String def check if self.length > 2048 puts "bad" else puts "good" end end end module Foo class Bar def initialize; end def baz "hello".check end end end include Foo bar = Bar.new bar.baz This runs fine. However, the only thing that should be able to access the new String#check method are objects of Class Bar. Right now, I can add the following to the main namespace: "goodbye".check and it will execute correctly, but I don't want this. I would like String#check to remain callable only for objects of type Foo::Bar. I thought I could achieve this by declaring String#check to be protected, since according to the Pickaxe book: "Protected: Can be invoked only by objects of the defining class and its subclasses." Well, "hello" is of type String, which is the defining class of String#check, so I expected to be able to call it. Instead, I get an error: ./foo:19:in `baz': protected method `check' called for "hello":String (NameError) I obviously don't understand protected and private methods very well yet :-( Any idea how I can restrict String#check so that it can only be called by objects of Class Bar? Ian -- Ian Macdonald | Your job is being a professor and ian@caliban.org | researcher: That's one hell of a good | excuse for some of the brain-damages of | minix. (Linus Torvalds to Andrew Tanenbaum) |