tags:

views:

1170

answers:

5

If I run this file as "ruby x.rb":

class X
end
x = X.new

What is the thing that is calling "X.new"?

Is it an object/process/etc?

A: 

It's the X class. You're naming the method "new" that creates and object of class X. So, if you run this text as a script, ruby

  • creates a new class X which is a subclass of Object, and which automatically (as a subclass of Object) inherits some methods, of which new is one.
  • sets up a name x
  • calls the new method on that new class X, creating an X object; x gets a reference to that object.
Charlie Martin
i think he's referring to an entry point of some sort.
Geo
+1  A: 

It's the ruby interpreter running the line

x = X.new

As with many scripting languages, the script is interpreted from top to bottom rather than having a standard entry point method like most compiled languages.

workmad3
+6  A: 

Everything in Ruby occurs in the context of some object. The object at the top level is called "main". It's basically an instance of Object with the special property that any methods defined there are added as instance methods of Object (so they're available everywhere).

So we can make a script consisting entirely of:

puts object_id
@a = 'Look, I have instance variables!'
puts @a

and it will print "105640" and "Look, I have instance variables!".

It's not something you generally need to concern yourself with, but it is there.

Chuck
Some of the other answers are saying that the "ruby interpreter" is calling the new method. But I think you're saying that there's an intermediate step before that happens. And that is that an instance of Object is created and all execution is mediated through that object. I'm really just trying to fill in the blanks in my understanding of how the "new" method gets passed as a message to the X object. From what you're saying it seems the main (Object instance) passes the new method as a message to the X Class instance. Am I getting closer?
lorz
Well, I guess technically you can say it's the Ruby interpreter that does *everything*, since it's what actually executes your code. But from the standpoint of the language, yes, your description sounds exactly right. At the top level, when you first start typing in Ruby, you're in the context of that object. Most people don't use the top level like an object, but it is one.
Chuck
A: 

As Charlie Martin said, X.new is a call to the constructor on the X class, which returns an object of type X, stored in variable x.

Based on your title, I think you're looking for a bit more. Ruby has no need for a main, it executes code in the order that it sees it. So dependencies must be included before they are called.

So your main is any procedural-style code that is written outside of a class or module definition.

Tim Hoolihan
Do ruby -e "puts self". It will print "main". It's the top-level context object.
Chuck
my point was simply that you don't declare a main method, as the original poster seemed to imply in his question.
Tim Hoolihan
+2  A: 

The top-level caller is an object main, which is of class Object.

Try this ruby program:

p self
p self.class
Igor Krivokon