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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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...


In reply to Re (tilly) 2: One more perl programmer's take on Ruby (discussion) by tilly
in thread One more perl programmer's take on Ruby (discussion) by deprecated

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found