Why does Ruby have Numeric.new?
a = Numeric.new # doesn't take an argument
I ca开发者_Python百科n't imagine a use case for this. Can you?
The Class
class defines a new
instance method. And so the new
class method on Numeric
is just a holdover from that - it doesn't do anything - think of it as one of those vestigial organs that animals inherit from a distant ancestor - like the appendix on humans.
Note that the subclasses of Numeric
such as Fixnum
and Float
and their kin explictly undefine the new
method. I guess they just didn't bother undefining it for Numeric
as direct instances of this class never really exist, and it does no harm keeping it around.
Everything in Ruby is an object, even classes. So to not have a constructor for Numeric
would mean there is no Numeric
class at all!
Take a look at the docs. It's simply a base class. You would never (well, it would be highly unlikely, anyway :)) use it directly.
You will never use numeric class in that fashion. It's the base class to all numeric types in Ruby.
Ruby’s numeric classes form a full numeric tower, providing many kinds of representations of numbers and numerical representations.
Source: Ruby Tips: Numeric Classes
精彩评论