[FAQ] Interpreted vs compiled [FAQ] defining methods anywhere

Hello,
Gavin suggested I compile the answers to my questions into FAQs. Here
they are:

1.- [FAQ] Is Ruby interpreted or compiled?

Ruby is interpreted - The parser creates an a syntax tree that is walked.
Future plans for Ruby are to move it closer to a compiled language.
These include projects to create a RubyVM (Rite, or Ruby 2.0) as well as
Cardinal (Ruby frontend for the ParrotVM).
Being interpretted does affect 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.

2.- [FAQ] Why can’t I define my methods anywhere?

Methods are defined at run-time. Therefore, (unlike Perl) a method cannot
be used before it is defined. This will not work:

greeting(“Daniel”)
def greeting(name)
puts “Hello #{name}”
end

But this will:

def greeting(name)
puts “Hello #{name}”
end
greeting(“Daniel”)

This is a trade-off. It allows you great freedom to modify methods during
program execution. For instance:

#!/usr/bin/ruby

define greeting(name)
puts “Hello #{name}”
end
greeting(“Daniel”)

define greeting(name)
puts “Hi #{name}”
end
greeting(“Daniel”)

Will print:

Hello Daniel
Hi Daniel

Whereas, the Perl equivalent:

#!/usr/bin/perl

sub greeting {
print “Hello $_[0]\n”;
}
greeting(“Daniel”)

sub greeting {
print “Hi $_[0]\n”;
}
greeting(“Daniel”)

Will print:

Hi Daniel
Hi Daniel

Because Perl’s subroutines are defined at compile-time.

In Ruby you als ohave the optino of using classes:

class Greeting
def initialize
hello
end

def hello
  puts "Hi!"
end

end

Greeting.new # → Hi!


_ \ __ _ _ __ _ _____| | / _\ __ _ _ _ _ _ _____ _ _ __ _

\ |/ | '_ \| | ___| | | | / _ | '/| '/| | '// ` |
/ | (| | | | | | || |
| |
| (
| | | | | | || || (| |
/ _,|| ||
||____| _/ _,|| || |__|| _,_|
Graduate Teaching Assisant. University of Maryland. (301) 405-5137

  • Daniel Carrera (dcarrera@math.umd.edu) [21 Nov 2002 16:07]:

[…]

In Ruby you als ohave the optino of using classes:

class Greeting
def initialize
hello
end

def hello
  puts "Hi!"
end

end

Greeting.new # → Hi!

This seems a bit ‘tacked on’ and not really relevant to the rest of the
question. Was there a specific reason classes were mentioned?

cheers,

···


Iain.

Daniel Carrera dcarrera@math.umd.edu writes:

Gavin suggested I compile the answers to my questions into FAQs. Here
they are:

I hope you don’t mind, but I’ve copied these into faqtotum.

Cheers

Dave

Well, it shows a meethod being called before it is defined,
but he doesn’t explain how methods in an explicit class
are different than methods in the Object class.

···

On Thursday, 21 November 2002 at 14:18:12 +0900, Iain ‘Spoon’ Truskett wrote:

  • Daniel Carrera (dcarrera@math.umd.edu) [21 Nov 2002 16:07]:

[…]

In Ruby you als ohave the optino of using classes:

class Greeting
def initialize
hello
end

def hello
  puts "Hi!"
end

end

Greeting.new # → Hi!

This seems a bit ‘tacked on’ and not really relevant to the rest of the
question. Was there a specific reason classes were mentioned?


Jim Freeze

F u cn rd ths u cnt spl wrth a dm!

Daniel Carrera dcarrera@math.umd.edu writes:

Gavin suggested I compile the answers to my questions into FAQs. Here
they are:

I hope you don’t mind, but I’ve copied these into faqtotum.

That’s exactly what they’re there for, Dave. Posts with subject [FAQ] are
intended to be added, after people have had a chance to comment on them. The
following -talk messages also need to be added, I believe:

56177
55654

We’re acquiring FAQs at a pretty good rate.

BTW, is there a way to view all contents at once in the FAQ? While I like the
FAQ webpage, FAQs are also good if they can be distributed, scanned, and read
at leisure.

Gavin

···

From: “Dave Thomas” Dave@PragmaticProgrammer.com

[snip]

This seems a bit ‘tacked on’ and not really relevant to the rest of the
question. Was there a specific reason classes were mentioned?

Well, it shows a meethod being called before it is defined,
but he doesn’t explain how methods in an explicit class
are different than methods in the Object class.

It was just a though. I guess it is kind of extraneous. Just leave that
out of the FAQ.

Daniel.

Please explain how methods in an explicit class are different than
methods in the Object class.

···

On Thursday, November 21, 2002, at 12:20 AM, Jim Freeze wrote:

Well, it shows a meethod being called before it is defined,
but he doesn’t explain how methods in an explicit class
are different than methods in the Object class.


Jim Freeze

F u cn rd ths u cnt spl wrth a dm!

“Gavin Sinclair” gsinclair@soyabean.com.au writes:

intended to be added, after people have had a chance to comment on them. The
following -talk messages also need to be added, I believe:

56177
55654

'tis done.

BTW, is there a way to view all contents at once in the FAQ? While I like the
FAQ webpage, FAQs are also good if they can be distributed, scanned, and read
at leisure.

Not yet. I’m still playing with technologies.

Cheers

Dave

Mark Wilson mwilson13@cox.net writes:

Please explain how methods in an explicit class are different than
methods in the Object class.

They aren’t - it’s just a question of when they are called:

bert
def bert

end

Here, the call to bert is executed before the definition of bert: it
fails.

class Dave
def initialize
bert
end
def bert

end
end

Here the call to ‘bert’ in initialize is parsed, but not executed. The
method ‘bert’ is then defined.

Later when you execute Dave.new, the call to bert succeeds, because
the method is defined at that point.

The fact that method definitions are active code means you can do
things such as

if $DEBUG
def fred
puts “lots of logging”
do stuff…
puts “more logging”
end
else
def fred
do stuff…
end
end

Cheers

Dave

So, that would explain the example below:

class Fred
Fred.fred # this fails
def self.fred
puts “1”
end
Fred.fred # this works
end

<thinking_out_loud>
I guess I am now coming to the realization that when I define
a method at the top level, that somehow it is automatically
instantiated. …There has to be some type of parser difference,
or the following would work:

class Fred
def fred
puts “1”
end
fred # undefined local variable or method `fred’ for Fred:Class
end

</thinking_out_loud>

Can someone please explain?
Thanks

···

On Thursday, 21 November 2002 at 14:42:35 +0900, Dave Thomas wrote:

Mark Wilson mwilson13@cox.net writes:

Please explain how methods in an explicit class are different than
methods in the Object class.

They aren’t - it’s just a question of when they are called:

bert
def bert

end

Here, the call to bert is executed before the definition of bert: it
fails.

class Dave
def initialize
bert
end
def bert

end
end

Here the call to ‘bert’ in initialize is parsed, but not executed. The
method ‘bert’ is then defined.


Jim Freeze

Paul’s Law:
You can’t fall off the floor.

class Fred
  def fred
    puts "1"
  end
  fred # undefined local variable or method `fred' for Fred:Class

Well, at compile ruby has not found the local variable `fred' this is the
first part of the message

     undefined local variable `fred'

This mean that `fred' will be interpreted as a method call, i.e. `fred()'
and because you are at class level (i.e. self = Fred), it will try to call
the method Fred::fred

at runtime it has not found the method Fred::fred, this is the second part
of the message

   undefined method `fred'

end

At toplevel you have

pigeon% ruby -e 'p self, self.class'
main
Object
pigeon%

In the class you have

pigeon% ruby -e 'class Fred; p self, self.class; end'
Fred
Class
pigeon%

Guy Decoux

So,

class Fred
def fred; puts “1”; end
fred
end

is different than

def fred; puts “1”; end
fred

because self=main and not a class?

‘main’ doesn’t seem to be anything I can access directly.
I guess this goes back to my original assumption, the
top level is a special case. No?

···

On Thursday, 21 November 2002 at 23:11:35 +0900, ts wrote:

class Fred
def fred
puts “1”
end
fred # undefined local variable or method `fred’ for Fred:Class

pigeon% ruby -e ‘p self, self.class’
main
Object
pigeon%

In the class you have

pigeon% ruby -e ‘class Fred; p self, self.class; end’
Fred
Class
pigeon%


Jim Freeze

It’s a very __UNlucky week in which to be took dead.
– Churchy La Femme

class Fred
  def fred; puts "1"; end
  fred
end

is different than

def fred; puts "1"; end
fred

because self=main and not a class?

You have partially found :slight_smile:

At toplevel, self is an instance of Object

the top level is a special case. No?

Try this

   class Object
      def fred; puts "1"; end
      p self, self.class
      fred
   end

Object is a special class in the ruby hierarchy

Guy Decoux