In article Pine.GSO.4.44.0211191855480.3450-100000@hardy.math.umd.edu,
Hi,
I am a Perl hacker trying to get to know Ruby. I have two questions:
Welcome… I used to do Perl a few years back, then I found Ruby
- What is a good resource for learning Ruby in general? Is there a Ruby
equivalent to “Learning Perl”?
Well, since you know Perl I’d recommend the ‘pickaxe’ book: “Programming
Ruby: The Pragmatic Programmer’s Guide” by Dave Thomas and Andrew Hunt.
Also available online at:
http://165.193.123.250/book/index.html
(there’s currently a problem with the registration of the rubycentral
domain, hence the use of numbers in the URL)
The pickaxe is a great book for learning Ruby (if you’re familiar with
programming in general already) and it is also has a very good library
reference section.
- Where does Ruby lie in the interpreted vs compiled range?
For instance, Perl is not really interpreted. The file is compiled into
an object code which is then run. This causes a speed benefit, and allows
for some flexibility. Is Ruby like that? or it is JIT-compiled? or is it
truly interpreted, like the shell?
Ruby is interpreted - The parser creates an AST that is walked. There are
projects in the works to create a RubyVM (Rite, or Ruby 2.0) as well as
Cardinal (Ruby frontend for the ParrotVM). Being interpretted does effect
the speed of execution to some extent, but Ruby actually does pretty well
in the language shootout rankings. Generally not as fast as Perl, but not
too much slower.
I noticed that Ruby doesn’t let me use functions until after I define
them (unlike Perl). Why is that? I want to have the freedom to define
the functions anywhere.
But even with Perl, don’t you have to pre-declare the function prototype?
Yes, in Ruby you do need to have the function predeclared prior to using
it. That’s because there’s really no distinction between compile-time and
runtime in Ruby. This actually is a very nice feature: you can do things
like:
if RUBY_PLATFORM =~ /win/
require “Win32Defs”
elsif RUBY_PLATFORM =~ /nix/
require “UnixDefs”
end
OR, things like conditional inheritance or conditional mixins based on
some environment setting, for example:
#conditional mixin
class Foo
if ENV[‘FOO’] && ENV[‘FOO’] == “true”
require ‘foo’
include FooMod
elsif ENV[‘BAR’] && ENV[‘BAR’] == “true”
require ‘bar’
include BarMod
end
#rest of class definition…
end
It’s probably not as easy to do that sort of thing in Perl because there
is a seperate compile phase. But, I have to admit, it’s been a while so I
wouldn’t be surprised if there is a way of doing this in Perl.
Phil
···
Daniel Carrera dcarrera@math.umd.edu wrote: