From: "Peña, Botp" Date: 2002-12-09T17:13:12+09:00 Subject: Re: [YANQ] -yet another (ruby) newbie question -string#concat Hi sir David (aka dblack@candle.superlink.net [mailto:dblack@candle.superlink.net]): You enlightened last Monday, December 09, 2002 12:04 PM: >> Then #replace method is another thing I would avoid since the syntax/nami= ng >> has implications or assumptions. Syntax/Naming should be unambiguous as m= uch >> as possible, imho... Why can we not standardize on suffixing "!" on metho= ds >> that modifies objects? Are they too many? Though, this is not a big probl= em >> for me really, as of now, since I'm actually avoiding them.. :-) >Hmmm.... I wish I could figure out how to talk you out of that. I >think you're investing the concept of object-modification with much >more complexity than it has. I may change soon if I cannot live with it any longer :-) Remember, I am just talking about the "!"-methods, and I think, they are not too many.. >As for having all destructive/modifying methods use !: we'd end up >with an awful lot of !'s in our code :-) > > a =3D [1,2,3,4] > a.push!(5) # and a.push(5) would create a dup of a??? > a[-1]! =3D 6 # as in: a.[]=3D!(-1,6) > a.delete_if! {|x| x =3D=3D 3} > >etc., etc.... They are indeed too many. And "!" makes method names _ugly_. So basically, the question is how to recognize destructive methods (since I guess we cannot live/trust with "!" alone). > >There's a lot of previous discussion on this (the general ! question, >not concat specifically). I searched for "destructive modify >receiver" on ruby-talk and hit a few such threads. I would have prefered that there be NO destructive methods. So using your examples above, I would rewrite them as: a = [1,2,3,4] a = a.push(5) a = (a[-1] = 6) #errs since []= does not return array a = a.delete_if! {|x| x == 3} and, I'm actually doing ruby coding like this now (pls don't laugh :-): a = [1,2,3,4] b = a.dup #save a a = a.push(5).delete_if{|x| x==2} # modify a anytime p "a now is ",a p "a originally was ",b Imho, the = .method.... is more explicit and therefore less confusing. True, it needs more typing, but that is the price I have to pay of being very explicit (and less questions for me). Or maybe, how about a ruby directive/option that disables methods fr being destructive (so that nubies like me would have a playing field)? >David Kind regards, -botp