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

问题描述

如果我将此文件作为ruby x.rb"运行:

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

class X
end
x = X.new

调用X.new"的东西是什么?

它是一个对象/进程/等等吗?

Is it an object/process/etc?

推荐答案

Ruby 中的一切都发生在某个对象的上下文中.顶层的对象称为main".它基本上是一个具有特殊属性的 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.

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

08-23 13:10