Welcome to the Monastery | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Ruby and perl are quite alike, so I'll pick out a couple of differences that I find interesting.
One of the most important design decisions in ruby is that everything is an object and practically everything you can do with those objects is implemented as a method call, in fact, a lot of the functionality that in perl is implemented as operators and functions are just method calls in ruby:
Is more or less equivalent with
Except that every container can decide how to implement its each method, which means you can keep the ruby code exactly the same if you decide to switch from a standard array to another implementation (like requesting records from a database, or reading lines from a filehandle etc etc) *) Compared to perl, ruby makes extensive use of polymorphism, and less auto-conversion of objects. For instance, a "+" operator will concatenate strings and add numbers in ruby **) while in perl "+" will convert strings to numbers and add them - this is one of the reasons perl has so many operators: since they force conversions on their operands, you need "+" and ".", "*" and "x" etc. This difference will be even more apparant in perl 6. One of the advantages of doing it the way perl does, is that it takes less typing; you almost never have to explicitly convert from strings to numbers or back. One of the advantages of doing it the way ruby does, is that it keeps the basic language simpler (less operators) and if the API is good it can make it easier to inject your own object types into existing code/library calls. Also IMHO ruby's way scales better. See the container.each example above. Also, ruby has stable, light-weight multithreading compared to perl. This is, at the moment, my biggest reason for using ruby instead of perl for some programs - you can quite easily run a program with 200+ threads in ruby, while in perl, using 200 threads or forking 200 sub-processes tends to slow things down too much. If you want to know more, I suggest you get a good book on ruby; I like the pickaxe book. I have the second edition in paper, but the first edition is also freely available online.
*) Yes, you can tie an object to an array in perl, but that's not quite the same thing.
In reply to Re: Perl/Ruby comparison
by Joost
|
|