Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "main" in Ruby?

Tags:

main

ruby

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?

like image 857
lorz Avatar asked May 27 '09 20:05

lorz


People also ask

Is there a main in Ruby?

The Ice::Application Class in Ruby. You may also call main with an optional file name or an InitializationData structure. If you pass a configuration file name to main , the settings in this file are overridden by settings in a file identified by the ICE_CONFIG environment variable (if defined).

Do you need a main method in Ruby?

@Hauleth's answer is correct: there is no main method or structure in Ruby.

What is * args in Ruby?

In the code you posted, *args simply indicates that the method accepts a variable number of arguments in an array called args . It could have been called anything you want (following the Ruby naming rules, of course).

What is self keyword in Ruby?

self is a special variable that points to the object that "owns" the currently executing code. Ruby uses self everwhere: For instance variables: @myvar. For method and constant lookup. When defining methods, classes and modules.


1 Answers

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.

like image 154
Chuck Avatar answered Sep 26 '22 03:09

Chuck