Why NaN.class throws NameError?

Here is my sample code:

class Foo
Bar = 10
end

Foo::Bar # => 10
Foo::Bar.class # => Fixnum
10.class # => Fixnum

Float::NAN # => NaN
Float::NAN.class # => Float
NaN.class

uninitialized constant NaN (NameError)

class Foo
Bar = 10
end # => 10
Foo::Bar # => 10
Foo::Bar.class #=> Fixnum
Bar.class
NameError: uninitialized constant Object::Bar
from (irb):6
from /usr/local/ruby-1.9.2/bin/irb:12:in `’

NAN is namespaced under Float. So you can only access it through
Float::NAN.


Dheeraj K.

Dheeraj K. wrote in post #1119707:

NAN is namespaced under Float. So you can only access it through
Float::NAN.


Dheeraj K.

Float::NAN # => NaN .

Does it mean constant NaN holds the value also NaN ? As
Float::NAN.class gives us the class name back Float… I am confused
in this point…

Float::NAN.instace_of?(Float)

undefined method `instace_of?’ for NaN:Float (NoMethodError)

While Float::NAN.class # => Float, How does the above error come out?

Float::NAN.is_a?(Object) # => true

It seems NaN an instance but for class instance it is?

Dheeraj K. wrote in post #1119714:

Float::NAN is a constant which holds a special float value NaN

@Dheeraj You are right…But my confusion is why then instance_of? and
#class are going against of each other? :slight_smile:

Can you post the code you’re confused with?


Dheeraj K.

Float::NAN is a constant which holds a special float value NaN


Dheeraj K.

Dheeraj K. wrote in post #1119723:

Can you post the code you’re confused with?

See here :-

C:>irb --simple-prompt

Float::NAN
=> NaN

Float::NAN.class
=> Float

Float::NAN.instace_of?(Float)
NoMethodError: undefined method instace_of?' for NaN:Float from (irb):3 from C:/Ruby193/bin/irb:12:in

On 27 August 2013 15:34, Love U Ruby [email protected] wrote:

Does it mean constant NaN holds the value also NaN ? As
Float::NAN.class gives us the class name back Float… I am confused
in this point…

I /think/ that what is happening there is that the displayed text
=> NaN
is the result of ruby attempting to print the value of Float::NAN.
There is no constant NaN, it is just text displayed when it tries to
print the value of the Float constant Float::NAN. In other words
Float::NAN is a constant which is not actually any number at all and
when printed displays as NaN to show that it is not a number.

Colin

Dheeraj K. wrote in post #1119727:

You spelt it wrong.

instance_of?, not instace_of?

OMG!! Thanks for the pointer.

What value Float::NAN contains ? If there is no value assigned to the
constant,then on what instance that #class is called?

Float::NAN contains a special float value NaN.


Dheeraj K.

You spelt it wrong.

instance_of?, not instace_of?


Dheeraj K.