From: Gavin Sinclair Date: 2002-12-09T19:08:43+09:00 Subject: Re: [YANQ] -yet another (ruby) newbie question -string#concat From: "Pe�a, Botp" > > >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). No, they are _beautiful_. Repeat after me: _beautiful_ ;) > > > >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 :-): I can't help laughing, I'm afraid, but it's with the greatest of affection for yourself and all the useful questions you ask! > 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). There's nothing really confusing about objects being modified. All data can be modified, so why not the data in objects? I think you need to treat your symptoms like a fear of spiders: confront your fear, modify every object in sight until you are used to it ;) (just kidding, of course) > Or maybe, how about a ruby directive/option that disables methods fr being > destructive (so that nubies like me would have a playing field)? Now that's just being ridiculous! :D Anyway, you have two options at your disposal: - irb is an *excellent* playing field - freeze every object you create, then you can't modifiy them! Cheers, Gavin