From: Robert Klemme Date: 2009-09-01T22:38:10+09:00 Subject: Re: Private methods’ viability (was: What is the ruby conventions to name private method?) 2009/8/31 Shot (Piotr Szotkowski) : > Ryan Davis: > >> 4) Don't have private methods if you don't need them. >> 5) You don't need them. No, really. > > I’ll bite. Note: I consider myself a beginner Rubyist and I’d love to > hear your (and others’!) reasons for the above and/or against the below. :) > > I use private methods when I factor them out of my public methods (once > the public methods have decent spec coverage). This makes me think > harder on the (hopefully – minimal) public API of my objects and > prevents me from calling such methods ‘in just that single place > where it would be useful’; a method’s either useful to the outside > world (and should be promoted to public API with individual, descriptive > spec coverage) or not, period. Using private methods helps me keep > checks on this. +1 > I also use protected attr_readers so that my objects can tell whether > they == others of the same kind without exposing their instance > variables to the outside world; an attr_reader on an Array/Hash/Set > does not guarantee that the given Array/Hash/Set is not modifiable from > the outside world¹, hence I’d rather not expose such instance variables > more than necessary. -0 - you cannot really prevent access to internals and if you include attributes in the equivalence relation you usually want to access them from the outside. My 0.02 EUR... > ¹ It’s one of the few places where Ruby diverges from the principle > of least surprise; I can see certain lovely symmetry and simplicity > in attr_reader working the same for all variables, but I found the name > a bit misleading when I started to learn Ruby – my thoughts were that > it should either not be called a reader or should return a dup of the > variable in question… (I did get used to this since then, though, and > realised that other languages’ generic getters usually do the same.) This is not necessary as a general functionality because there are immutable classes, others can be frozen and last but not least sometimes you want to give someone access to internals. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/