Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: One more perl programmer's take on Ruby (discussion)

by elusion (Curate)
on Nov 18, 2001 at 06:13 UTC ( [id://126077]=note: print w/replies, xml ) Need Help??


in reply to One more perl programmer's take on Ruby (discussion)

Fellow Monks,
I have done some coding in Ruby. I've read the book also. (which is online here) Ruby does have a GUI, it uses Tk. It works well on Windows (That's what I code in), but I've heard it's faster in Unix. I've not written more than 600 lines, but I've really enjoyed doing it. I am one of the "for fun" programmers that brother dep mentions. I will elaborate a little be here about some of the joys I've had.

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.

foreach ($i=3;$i<19;$i++) { print $i; } 3.upto(18) do |i| print i end
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.
class Numbers def gimme 3.upto(10) { |number| yield(number) } end end Numbers.gimme { |number| puts number }
This would print 3\n4\n5\n6\n7\n8\n9\n10\n.

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
    As someone who has done stuff in both languages, here are some corrections on each topic you brought up.

    Regexp: In Perl you put the string on the left. In Ruby you can put the string on the left and the pattern on the right, or vice versa. Normally I would write it the same in both languages. What is going on is that =~ is a method of both String and Regexp, for no particular reason than to provide a little syntactic sugar.

    Ruby's threading is cooperative inside of the language. You could implement using class Continuation. This does not give you access to OS level threads. You cannot, for instance, use Ruby's threading and blocking system or database calls. So it is there, kind of. But it isn't really useful for anything I would want threads for.

    Loops In Perl you appear to still be using C-style for loops. Use more Perlish foreach loops and you will avoid most off by one errors. For instance your example is better written in Perl as:

    foreach my $i (3..18) { print $i; }
    (And I would write it similarly in Ruby.) However there is something nice about Ruby. You can loop over anything as long as it provides an each method and imports Enumerable. Why should there be a difference between looping over text you are reading from a file, and looping over text in a String? Matz didn't think there should be, and as a result you can loop over lines of text in a string directly with a foreach, and if you use a foreach on a file since it goes through each, you don't have the slurping problem you do in Perl. (The problem comes when you read a large file into memory then swap.)

    Blocks are just syntactic sugar around anonymous subroutines. You can do it in Perl. You just need to write a few sub declarations. But you won't get the core of the language to make widespread use of it without a significant rewrit. So Perl is able to do it, you just don't have the sugar.

    Operator Overloading Perl has this. See overload. However Ruby's overloading model is much more consistent and fine-grained than Perl's. Everything is an object. Redefine the methods you want. (Except for the comparison operator on a string for a sort. Grrr...)

    Class and Object variables This is one place that Ruby shines relative to Perl. Ruby's OO model is much cleaner, and writing classes and methods is so much nicer than in Perl.

    And now for the points you missed, good and bad.

    First the summary. Ruby is Smalltalk's OO model, with a grammar that is meant to look a bit like Perl's, and a class system designed around emulating Perl. Plus a few Lispish goodies thrown in because Matz likes them. As a result you have at your fingertips most of the basic data manipulation conveniences of Perl, but in a much simpler language, with a dynamic type system.

    But there are differences. Ruby is much simpler than Perl, much more consistent, has a lot of familiarity, and has a far cleaner OO model. Ruby's syntax and type system are separate, for instance write a hash access, array access, and call an anonymous function with foo[bar]. Therefore a large amount of Perl's syntax can be thrown out, while maintaining a far finer-grained type system than Perl has. For instance a Float and an Integer are different things in Ruby, and while with Integer arithmetic you will never overflow, with Floats you have the usual constraints. This also means no autovivification. Unlike Perl, Ruby has true garbage collection. This means a cleaner interior, fewer memory leaks, but you don't get deterministic destructors.

    And there are some bad points. First of all Ruby doesn't have CPAN. Secondly Ruby doesn't have any equivalent of strict.pm, and I definitely missed it. Thirdly Ruby has optional semi-colons at the end of lines, sometimes if you leave one off you can get a multi-line statement, and sometimes not. This is a design decision that complicates the grammar a lot and can make it hard to tell sometimes where a line ends. And last, but not least, Ruby is a much younger language than Perl. Expect to find bugs from time to time...

      Hey Tilly. If one knows Lisp, Smalltalk, and Perl (hey, I get to mention my three favorite languages all at once! :-) what new ideas would one learn from Ruby? I've had it on a (very) back burner for a while to look at, but the answer to that question hasn't been clear to me, so I've never gotten around to it, though I'd be glad to if I were convinced it would teach me something.

        You might learn how well the ideas fit together. Also even with exposure to Lisp you might not be familiar with mixins or the use of anonymous blocks in Ruby. Even with exposure to Perl, you might not realize how well it can be modelled with a class library. And even with exposure to Smalltalk, you might not realize how well the object model works even with a radically different class library.

        I don't know what specifically you would find new. But if you learn it, please tell me what you found out. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://126077]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found