From: "David A. Black" Date: 2009-10-11T19:13:26+09:00 Subject: Re: When use self.method_name and when only method_name? Hi -- On Sun, 11 Oct 2009, Joshua Muheim wrote: > Hi all > > Just a small newbie question: When calling a method inside an object, I > should always use self.method_name for good style, right? I know that I > could always only use "method_name", but it's a bit prone to errors > because I could have also a local variable called "method_name", and > when I use "self." I always call the method, and not a potentially > available local variable. > > So it's always better style to use: > > def validate > self.errors.add_to_base("...") > end > > instead of: > > def validate > errors.add_to_base("...") > end > > Right? :-) In general I prefer to leave off the optional "self." except in a couple of cases. The one that comes to mind mainly is: def <=>(other) self.age <=> other.age end where I like having the explicit receiver on both sides. It's definitely not unidiomatic to leave off the self. In fact, the whole private access level depends on it -- and even if you're not using a lot of private methods, that's enough to confirm that it's acceptable stylistically. David -- The Ruby training with D. Black, G. Brown, J.McAnally Compleat Jan 22-23, 2010, Tampa, FL Rubyist http://www.thecompleatrubyist.com David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)