-----Messaggio originale----- Da: Josh Cheek [mailto:josh.cheek / gmail.com] Inviato: venerd2 dicembre 2011 05:06 A: ruby-talk ML Oggetto: Re: Why are there so many similar/identical methods in core classes On Thu, Dec 1, 2011 at 9:15 PM, Kassym Dorsel <k.dorsel / gmail.com> wrote: > Let's look at the Array class and start with method aliases. > > There are two aliases : .to_s <=> inspect , .size <=> .length , index > <=> find_index. I understand the need for the first one, since > sometimes the two don't return the same thing depending on the class. > But why have size/length do the exact same thing..? This is one pretty ntuitive so it's not that bad.h > > Then we have methods that are defined together, but with different > names > : collect <=> map , [] <=> slice <=> at. > > Next we have methods that seem to be doing the same thing, but are > implemented differently : keep_if <=> select! , reject! <=> delete_if > , delete_at <=> slice! <=> shift > > Next are the nearly identical methods. They are the same except one > does a little : count <=> length/size > > Useless methods? : replace. Why do you need an explicit method for this. > Can't you just use the assignment operator ? > > These are just the ones I remember/found. I'm sure there are many > more examples in this class and others. Doesn't having multiple names > for the same thing make it confusing when collaborating ? Is there any eason for this ? What is your take on the matter ? > > -- > Posted via http://www.ruby-forum.com/. > > IDK for all of them. Some are actually different, to_s and inspect are quite different (and brillaint!). Others like map and collect are that way in order to appeal to different audiences. I think map comes from Lisp and Collect comes from Smalltalk. So these are both common names for this method, and thus they are both available. Same for size and length, and reduce and inject. Others I think are synonyms either for convenience, or because they make more sense in different contexts. I'd expect most of the operators are defined for convenience (ie there is a method that does this, but b/c Ruby allows operator overloading, they realize the operator would make a lot of sense if it did this same thing, and are thus aliased). Ruby generally adheres to TIMTOWDI, but I find I usually settle on a preferred version, though it is sometimes fun to play with alternatives. -- Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f Sponsor: ING DIRECT Conto Arancio. 4,20% per 12 mesi, zero spese, aprilo in due minuti! Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid921&d)-12