Ruby: BigDecimal: 同时有一个 class 和一个方法?

Ruby: BigDecimal: a class and a method at the same time?

require bigdecimal
BigDecimal.class     # => Class

所以,BigDecimal 是 class。

但同时BigDecimal可以像方法一样调用:

BigDecimal("42.0")   # => 0.42e2

背后的机制是什么?我怎样才能看到 BigDecimal 是可调用的?我怎样才能使我自己的 class 常量可调用?

BigDecimal是一个class,但也是Kernel模块中定义的方法

内核中定义的方法不必调用接收器,因为 Kernel 模块与 Object class.

From the docs: The Kernel module is included by class Object, so its methods are available in every Ruby object.

Ruby 知道 Array.newArray(1) 是不同的东西,因为常量 (classes and modules are constants) 不接收参数。

class Abc
end

def Abc
  puts 'Method called'
end

Abc()
#=> Method called

Abc
#=> Abc (Class)

正如Matz曾经说过的:

I'm trying to make Ruby natural, not simple. Ruby is simple in appearance, but is very complex inside, just like our human body.