[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Protected methods and class methods
From:       Gioele Barabucci <ml () gioelebarabucci ! com>
Date:       2006-01-22 17:56:43
Message-ID: 200601220017.09306.ml () gioelebarabucci ! com
[Download RAW message or body]

On Saturday 21 January 2006 01:46, Ross Bamford wrote:
> On Sat, 2006-01-21 at 04:34 +0900, Gioele Barabucci wrote:
> > Now I'm facing a problem. I use some static methods as "factory methods",
> > to create "prefilled" class instances. These methods can't access the
> > protected methods of the same class. Is this behavior intentional?
>
> I believe so. Because the class Info is itself an instance of Class, 
> while the instance is an instance of Info, which is entirely unrelated
> to Class (apart from the common ancestor, Object). So there's no reason
> for protected instance methods on Info to be available to methods on the
> class itself.
Instead it makes sense to me to be able to use Info protected methods from 
Info class methods. I see no need for 'Class' methods to be able to use 
protected members of an arbitrary class, but this is a particular case! Both 
methods are strictly related to Info (and its subclasses, but this is just a 
detail).

> (I think static methods are usually referred to as class methods in
> Ruby. *nothing* is static here).
Ah, my C++ heritage :)

> Maybe try this workaround.
>
>     info = Info.new(type_id)
>     info.instance_eval { self.length = len }  ## <<< changed!
>
> Hope that helps,
> Ross
Yes, it worked fine. Just I'm not so happy when I have to use *_eval :( I feel 
like cheating. Anyway it is better to have such a shortcut than not :)

--
Gioele <dev@gioelebarabucci.com>

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic