Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by tilly (Archbishop)
on Nov 18, 2001 at 09:51 UTC ( [id://126094]=note: print w/replies, xml ) Need Help??


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

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...

Replies are listed 'Best First'.
Re: Re (tilly) 2: One more perl programmer's take on Ruby (discussion)
by hding (Chaplain) on Nov 18, 2001 at 18:54 UTC

    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. :-)

        Just to satisfy my own curiousity and see if I really should take a closer look at Ruby - Are the mixins of Ruby significantly different than mixins one would typically use with CLOS? Are the anonymous blocks different than blocks in Smalltalk or lambda forms in Lisp?

        I do find the last point somewhat interesting - it would be interesting to a different class library than a typical Smalltalk's (though some of those are so feature-filled it's hard to imagine needing more :-).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-29 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found