在 javascript 中,您可以创建不是任何实例的空白对象。像这样:
foo = {
bar: 12,
fooFunc: function() {
return this.bar;
}
}
在 ruby 中这(或类似的东西)可能吗?
最佳答案
Ruby 有一个 Object
class所有其他对象都从中继承。来自 Ruby 文档:
Object is the parent class of all classes in Ruby. Its methods are therefore available to all objects unless explicitly overridden.
Object mixes in the Kernel module, making the built-in kernel functions globally accessible.
在 Ruby 1.9 中:
Object.new.class #=> Object
Object.class #=> Class
Object.superclass #=> BasicObject
BasicObject.class #=> Class
BasicObject.superclass #=> nil
关于javascript - Ruby - 你能像在 javascript 中那样创建一个独立的对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17139433/