什么是“主要"?在Ruby中? [英] What is "main" in Ruby?

查看:124
本文介绍了什么是“主要"?在Ruby中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我以"ruby x.rb"的身份运行此文件:

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

class X
end
x = X.new

什么叫"X.new"?

是对象/进程/等吗?

推荐答案

Ruby中的一切都发生在某个对象的上下文中.顶层的对象称为主要".基本上,它是具有特殊属性的Object的实例,在那里定义的任何方法都将添加为Object的实例方法(因此它们随处可见).

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

它将显示"105640"和看,我有实例变量!".

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.

这篇关于什么是“主要"?在Ruby中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆