From: ChrisO Date: 2004-09-16T07:49:52+09:00 Subject: Re: Ex-Perl coders: Howz it feel to convert to Ruby? Brian Wisti wrote: > Hmm ... TMTOWTDI applies in both Perl and Ruby. I think I would have > written most of your examples differently, and that difference is > probably why Perl<->Ruby isn't a big deal for me. > > >>(1) The complete OO nature of Ruby IMO changes everything. Perl is >>still procedural in it's basic constructs. To iterate from 1 to 10 >>in >>Perl can be shortened *somewhat* from say C or Java using this >>construct: >> >>for (0..9) { do_somthing_with( $_ ) } > > > foreach my $i (0..9) { do_something_with($i) } > You know, a stylistic thing for sure, but I've stopped using "foreach" and just use "for" everywhere. And if I can get away with using $_, I do in lieu of the "my $i" unless I have inner loops and I need a variable to ferret out abiguity. But I'm OT here, discussing Perl sytle in a Ruby NG. > or even > > map { do_something_with($_) } (0..9); > > >>10.times { |i| do_something_with( i ) } > > > 0.upto(9) { |i| do_something_with(i) } > > >>To me, this is a significant difference and I think this gets >>magnified >>the deeper one goes into Ruby. > > > True enough, but presumably your understanding deepends as you go > deeper into Ruby, yes? > I would agree with that. > >>(2) I think the need to be familiar with the class methods and >>properties is magnified in Ruby. This is similiar to C++ where it's >>difficult to be productive unless one gains experience in the classes >> >>and methods available. In Perl, this is not as much the case, again >>do >>to the fact you can go completely procedural in Perl and get away >>with >>it. If you do use OO and modules in Perl, then you only need to know >> >>the methods for the module you imported and the rest can be >>procedural >>in Perl. In Ruby, it seems you can't really get away with that it >>seems. > > > Sure, but POLS is a decent guideline, and when all else fails I call > obj.methods() from irb to see what I get to play with. I generally > don't need to refer to docs for libraries that I haven't imported, so > I'm not sure I understand that bit. > irb is nice just as "perl -e" on the command line is for checking out features. I've done both as well as "ruby -e". > And maybe things aren't as straightforward in Perl as you describe. One > still has to be familiar with the full standard library, the various > sigils, the built-in types, and all of the modules that you've > imported. > I can't argue too much that Perl is kind of it's own thing too, yes. But it's still easy to keep completely procedural and I would say a large amount of Perl that I see is. > >>(3) Another contrived example, this time in Ruby: >> >>def Contrived >> 3.times { yield } >>end >> >>[ "tic", "tac", "toe" ].each do |item| >> Contrived { printf "%s ", item } >> printf "\n" >>end >> >>There is a lot about this that is different than Perl IMO. I could >>create an anonymous subroutine in Perl to function something like the >> >>yield block, and I realize the Ruby above is itself somewhat >>contrived, >>but... still, you wouldn't normally do something like this in Perl >>whereas in Ruby, these sorts of things are actually useful. > > > Useful, sure. Widely used, you betcha. Fact is that I've still never > needed to use yield in any production code. Use what you need, not what > you feel like you're supposed to. Your sample Perl code didn't show > taint-checking or full conformance to strict and warnings, even though > that is widely promoted as the "right way" to do things. > > >>(4) Each language has it's own strengths. IMO one of Perl's >>strengths >>is the simplicity and consistency of it's parameter passing: >> >>sub shell { >> >> my @output; >> for (@_) { push @output, `$_` } >> chomp( @output ); >> return wantarray ? @output : \@output; >> >>} >> >>## My favorite "Rubyism" I use all the time in Perl -- puts()! >> >>sub puts { for (@_) { print "$_\n" } } >> >>puts( shell( >> "ls -l", >> "ps -ef | grep -i foo", >> "rm *.bar", >>)); > > > Yup, it's a strength of Perl, and that's a nifty puts subroutine :-) > Thanks. It's tremendously useful for a ton of things written this way. I still find uses for it that surprise and delight, not to mention that I have "puts()" in every language I use -- C, JavaScript, Perl, VB, WSH, etc. I don't even have to remember any more, except I can't do the above (with the array as a parameter) in anything but Perl. > >>So the point is I don't want to approach Ruby as Perl even though >>there >>are similiarities. Unfortunately, I see most people approach Perl in >> >>the same way as C and Java and JavaScript and Perl really looses it's >> >>benefits quickly when approached in this way. I don't want to do the >> >>same with Ruby. > > > What's wrong with taking advantage of the languages that Ruby builds > on? Familiarity with Perl, Python, Smalltalk, and LISP are all > incredibly useful with Ruby, and sometimes a design is easiest to > express in something resembling one of those languages. > > I can see a problem with trying to make your Ruby code look *exactly* > like Perl code, same as writing a C-style for(;;) loop instead of a > for/foreach loop in Perl is ugly. But hey, sometimes it's useful. I > don't knock what works, as long as I can read it :-) > > >>Perhaps these weak examples answer your question. And so the >>original >>question, clarified more (more muddied more as the case may be), >>remains... Howz it feel to convert to Ruby _really_ and not just >>applying the "Perl" mindset to Ruby, which I think is a mistake. >>It's >>clear from even the basic constructs it isn't the same. > > > Sure, it's not the same. But insisting on doing things the "Ruby Way" > immediately is naturally going to result in a serious dip in > productivity compared to going out and writing code that works, right > away. > This is a perspective I need to adopt more, for sure. It's probably the only realistic way and was exactly the same way I learned Perl. I didn't develop that puts() call above overnight, as simple as it is, simply because my ability to conceptualize it that easily didn't exist until after I had written a significant amount of code. The same will be true for Ruby. -ceo