From: "Jesús Gabriel y Galán" Date: 2010-04-01T01:33:03+09:00 Subject: Re: RubyDictionary - First Try On Wed, Mar 31, 2010 at 6:23 PM, Josh Cheek wrote: > 2010/3/31 Jesús Gabriel y Galán > >> 1.- >>        def initialize(entries=nil) >>                if entries==nil >>                        @entries = Array.new >>                else >>                        @entries = entries >>                end >>        end >> >> this could be done like this: >> >>        def initialize(entries=nil) >>           @entries = entries || []   # or Array.new if you prefer >>        end >> >> How about > def initialize( entries = Array.new ) >  @entries = entries > end Yep, that's cleaner. > BTW, I think it is better to use setters and getters than to directly access > the ivars, because if you restrict access to the method, then you only have > to go to one place if you change implementation. Sounds good to me, enter the Self Encapsulate Field refactoring (yes, I'm now reading Fowler's book :-) attr_accessor :entries def initialize(entries = Array.new) self.entries = entries end Although maybe you want the accessor to be private at first, unless your public interface calls for that. Jesus.