tags:

views:

116

answers:

3
a = Numeric.new  # doesn't take an argument

I can't imagine a use case for this. Can you?

+1  A: 

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.

thenduks
This is Wrong. Many Ruby classess do not have the `new` method, see: TrueClass, FalseClass, NilClass, Fixnum, Float, etc
banister
Eh, a minor detail. A class can undefine it's `new` method if it wants. Numeric doesn't because you're meant to be able to subclass it and use it as a base yourself. `TrueClass` and the like aren't meant to be used like this.
thenduks
that 'minor detail' makes this bold statement of yours completely invalid: "So to not have a constructor for Numeric would mean there is no Numeric class at all!"
banister
Just because there is no constructor _now_ doesn't mean it's possible to not have one at all. If a class has no constructor and falls down in the woods, does it make an instance? Perhaps I've worded my answer a bit 'boldly', but... yea, there is a constructor for TrueClass and NilClass, they've just been undefined explicitly, wheras for Numeric it hasn't. With no constructor at all the class might as well not exist in the first place.
thenduks
+1  A: 

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

zengr
But you didn't explain why `new` still exists :)
banister
+3  A: 

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.

banister
Yes, that's how I found out. played around in irb, trying Float.new('NaN'), failing (NoMethodError). Going for Numeric.new("NaN'), ending up with a useless(?) Numeric.
steenslag