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 Kumar wrote in post #1119707:

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

-- Dheeraj Kumar

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 is a constant which holds a special float value NaN

Dheeraj Kumar 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 Kumar 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 `<main>'

You spelt it wrong.

instance_of?, not instace_of?

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 Kumar wrote in post #1119727:

You spelt it wrong.

instance_of?, not instace_of?

OMG!! Thanks for the pointer.

What value `Float::NAN` contains ?

Float::NAN contains a special float value NaN.