Learning Ruby

Hi,

I am a Perl hacker trying to get to know Ruby. I have two questions:

  1. What is a good resource for learning Ruby in general? Is there a Ruby
    equivalent to “Learning Perl”?

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

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.

Thanks,
Daniel.

Hi –

Hi,

I am a Perl hacker trying to get to know Ruby. I have two questions:

Welcome :slight_smile:

Partial response:

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.

Pretty much everything happens in lexical sequence in Ruby, so if you
ask for a method to be called, it has to exist. As a corollary, you
can also redefine methods:

def my_method
puts “hi”
end

my_method # hi

def my_method
puts “hello”
end

my_method # hello

This is a pretty dinky example – but you can see that having a call
to my_method before any version of it was defined wouldn’t make sense.

Nor is it possible to do some kind of lookahead where the last
definition of a method is known in advance. Method definition may be
conditional on runtime state, so there’s no way to anticipate which
definition is “last”.

Another way to look at it is: a Ruby object responds to messages
(i.e., method names), and the set of messages it can respond to is
subject to change during the life of the object. Therefore, there’s
no way to do method existence lookahead. The transaction – the
method call – always succeeds or fails based on the capacities of the
object at the time of the call.

David

···

On Wed, 20 Nov 2002, Daniel Carrera wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Daniel Carrera wrote:

  1. What is a good resource for learning Ruby in general? Is there a Ruby
    equivalent to “Learning Perl”?

“Programming Ruby”, by Dave Thomas and Andy Hunt.

  1. Where does Ruby lie in the interpreted vs compiled range?

My understanding (which may be way off) is that the code is parsed into
a syntax tree and that the tree is then walked – it’s never translated
into bytecode, though. So it’s somewhere past interpreted but short of
what you described for Perl. Note that future plans for Ruby are to move
in that direction, however.

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 :wink:

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

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

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.

Thanks,
Daniel.

I think one trick is to do this:

hello()

BEGIN {
def hello
puts “Hi!”
end
}

but I’m not sure :slight_smile:

I almost always code in classes, which tends to avoid this problem:

class Example
def initialize
hello
end

def hello
  puts "Hi!"
end

end

Example.new # → Hi!

Gavin

···

From: “Daniel Carrera” dcarrera@math.umd.edu

I am also new to Ruby (and to programming in general). I have found
the book “Teach Yourself Ruby in 21 Days” , by Mark Slagell, to be very
helpful (it was my second book after “Programming Ruby”. There is also
a Ruby User’s Guide online at http://www.ruby-lang.org/~slagell/ruby/.

···

On Tuesday, November 19, 2002, at 10:51 PM, Phil Tomson wrote:

In article
Pine.GSO.4.44.0211191855480.3450-100000@hardy.math.umd.edu,
Daniel Carrera dcarrera@math.umd.edu wrote:

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 :wink:

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

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

I noticed that Ruby doesn’t let me use functions until after I define
them (unlike Perl)…

But even with Perl, don’t you have to pre-declare the function
prototype?

Nope. The following works:

#!/usr/bin/perl
hello(“Daniel”);

sub hello {
$name = $_[0];
print “Hello $name\n”;
}

Yes, in Ruby [snip] you can do things like:

if RUBY_PLATFORM =~ /win/
require “Win32Defs”
elsif RUBY_PLATFORM =~ /nix/
require “UnixDefs”
end

OR, [snip]
if ENV[‘FOO’] && ENV[‘FOO’] == “true”
require ‘foo’
include FooMod
elsif ENV[‘BAR’] && ENV[‘BAR’] == “true”
require ‘bar’
include BarMod
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.

Actually, the same things would work with Perl’s “require”, but AFAIK not
with “use”.

But this points to a difference between Perl and Ruby. Take the following
Perl code:

sub hello { print “hello” }
hello;
sub hello { print “hi” }
hello;

This will print:

hi
hi

Where as the Ruby version would print

hello
hi

So I can see a bit of a trade-off. Ruby allows me to redefine functions
at run-time, which can be very cool. For instance, I can make a
“vector” class, and redefine the method “+” several times to correspond to
different metrics.

Thank you all for the help. The resources are very good.

Daniel.