47

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?

lorz
  • 1,099
  • 1
  • 9
  • 14

5 Answers5

53

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
  • 228,856
  • 29
  • 291
  • 386
  • 1
    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 May 27 '09 at 20:37
  • 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 May 27 '09 at 20:54
  • @steel Do you know where this magical behavior is documented? – Jonah May 18 '20 at 01:59
  • From the standpoint of the language? From the standpoint of Smalltalk? Maybe yes, the sender is the `main` object. Not sure, and not sure how applicable all that is here. Surely Ruby is influenced by Smalltalk. But to send a message explicitly we do `X.send(:new)`, not `send(X, :new)`. Neither that is the case in Smalltalk. And we have the [`caller` method](https://ruby-doc.com/core-2.5.5/Kernel.html#method-i-caller), not `sender`. So I think involving Smalltalk is far-fetched. And that's the terms I suggest to think in. `main` is the caller. `main` calls method `new` on class `X`. – x-yuri May 28 '21 at 09:12
  • I feel like w/o the other [question/answer](https://stackoverflow.com/a/917311/52499) this one makes less sense. – x-yuri May 28 '21 at 09:13
13

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

Try this ruby program:

p self
p self.class
Igor Krivokon
  • 10,027
  • 1
  • 36
  • 41
3

It's the X class. You're invoking the method "new" that creates an 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 instance object; x gets a reference to that object.
MarkDBlackwell
  • 1,894
  • 18
  • 27
Charlie Martin
  • 107,118
  • 23
  • 189
  • 257
2

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
  • 24,570
  • 4
  • 34
  • 56
  • Yeah, I think the person asking this question was referring to `main` from the perspective of a C programmer. Everyone else here seems to be thinking of the top-level object named `main` in Ruby, which isn't quite the same thing. – Ajedi32 Jul 01 '15 at 14:57
1

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
  • 12,268
  • 3
  • 40
  • 54