From: "David A. Black" Date: 2008-06-15T22:03:06+09:00 Subject: Re: why can't an instance instantiated within a class method access a protected instance method? Hi -- On Sun, 15 Jun 2008, Florian Gilcher wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > > On Jun 15, 2008, at 12:37 PM, Greg Hauptmann wrote: > >> (complete email)Hi, >> >> Why can't an instance instantiated within a class method access a protected >> instance method? >> >> e.g. >> >> class Recurring < ActiveRecord::Base >> >> def self.reconcile rs = Recurring.find(:all) >> rs.each do |r| >> r.reconcile_one >> end >> end >> >> protected >> >> def reconcile_one >> # do something >> end >> >> end > > Because calling a Method with an explicit receiver is like "calling from the > outside". It's not quite that. Protected methods can be called with an explicit receiver, but they can only be called on objects that are of the same class as self (or a subclass thereof). In self.reconcile, self is Recurring, and therefore of class Class, while rs is of class Recurring. David -- Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS June 16-19 Berlin ADVANCING WITH RAILS July 21-24 Edison, NJ See http://www.rubypal.com for details and updates!