in reply to One more perl programmer's take on Ruby (discussion)
When they call Ruby "Perl's younger, prettier sister," they speak the truth. The syntax is a clean and, for the most part, concise. When it's not the most concise, I find that it is because a little bit of sugar has been added instead. I don't think that it is very comparable to Python. I've looked at Python and have decided it is overrated. Ruby, on the other hand, is nice. Here are some of the things I like:
Regexp: Ruby provides a builtin Regexp (notice different spelling) class. It is just like Perl's, except you would say /pattern/ =~ "string" instead of "string" =~ /pattern/. Ruby also fills all the regex variables ($`, $1, $2, etc.) like Perl.
Threading: Although I've not done any threading in any langauge, it seems to be the consensus that Ruby makes it surprisingly simple.
Loops: I really like Ruby's loops. They are one of the occasions where a little bit of typing has been added to make things cleaner. The first example is Perl, the second is Ruby.
Blocks: This is something different about Ruby. You can pass a block of code to a method, and, at a specified point, the method will execute the block with a variable(s) that is passed to it. This is actually how the .each method works for the Array class in Ruby.foreach ($i=3;$i<19;$i++) { print $i; } 3.upto(18) do |i| print i end
This would print 3\n4\n5\n6\n7\n8\n9\n10\n.class Numbers def gimme 3.upto(10) { |number| yield(number) } end end Numbers.gimme { |number| puts number }
Operator Overloading: This isn't something that is uncommon in OO langauges, but it is lacking, as far as I know, from Perl's. Take this as an example: You're working on image manipulation. This requires you to create a list of lists. We'll assume that we've already made the lists. In Ruby you would have to write a simple 10-15 line class for this example. To access this in Perl, you would write $image[$x][$y]. In Ruby you could make it do this image[x,y]. True, there's not much difference, but with lots of code this can really make things more readable.
Class and Object variables: Ruby provides a nice clean interface to different types of variables. To have object variables in Perl, you must have a, in my opinion ugly, %self that contains all your info and is then passed to all the methods. In Ruby, this is much cleaner. All object variables are simply prepended with an @. All class variables, which are the same for all objects in that class, are prepended by 2 @'s. All global variables are prepended by a $. This may look confusing at first for a Perl programmer. Whenever I see a variable with an @ in front, I want it to be an array. But you get over that fairly soon.
Similarity: Besides all these differences, and some more, Ruby is very like Perl. As Deprecated mentioned, many things are very intuitive, or actually the same, to a Perl programmer.
This doesn't mean that I will be abandoning Perl at all. In fact, coding Ruby has actually made me like Perl more. It is really good to shake things up once in a while. Perl just has something about it though. Maybe this is because it's my first programming language. I don't know, but I'd encourage everyone, especially the "for fun" programmers, to take a look.
elusion : http://www.elusion.f2s.com
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: One more perl programmer's take on Ruby (discussion)
by tilly (Archbishop) on Nov 18, 2001 at 09:51 UTC | |
by hding (Chaplain) on Nov 18, 2001 at 18:54 UTC | |
by tilly (Archbishop) on Nov 19, 2001 at 07:52 UTC | |
by hding (Chaplain) on Nov 19, 2001 at 19:50 UTC | |
by tilly (Archbishop) on Nov 19, 2001 at 20:56 UTC |