From: Jari Williamsson Date: 2008-02-27T03:43:53+09:00 Subject: Re: Monkeypatching is Destroying Ruby Joel VanderWerf wrote: > Jari Williamsson wrote: >> John Wilger wrote: >>>> The disadvantage of making Array#to_csv is that you are >>>> modifying a global "variable" (the Array class). >>> >>> I'll take that disadvantage (in combination with good test coverage!) >>> over having to write verbose, type-checking code any day. >> >> IMO, this is not an either/or case. Why not just add to_csv() only to >> the instance who actually could need it? > > How far in advance do you know that an instance needs this method? > > If you wait too long, you end up with another case statement (again, on > the class of the object, yuck) to decide which #to_csv is appropriate > for the object. > > If you add #to_csv eagerly, then the requirement that this object > respond to #to_csv becomes a strong coupling between the point of > creation and the #i_need_csv implementation. I don't think it would matter in this case. You should be able to create a dynamic implementation without case statements, since a CSV implementation would be dependent on the #to_a method? (And #to_a for an array object just returns self.) Best regards, Jari Williamsson