On Mon, 2006-01-23 at 02:56 +0900, Gioele Barabucci wrote:
> 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).
> 

Yes, I can see the logic in that, and I don't disagree that it'd
probably make sense in some respects, but now I've become more familiar
with Ruby I think I probably _would_ find it surprising if protected
methods worked that way. Things are often more simple (and elegant) than
you'd imagine I suppose...

> > Maybe try this workaround.
> >
> >     info = Info.new(type_id)
> >     info.instance_eval { self.length = len }  ## <<< changed!
> 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 :)

Definitely :)

-- 
Ross Bamford - rosco / roscopeco.REMOVE.co.uk