Why NaN.class throws NameError ?

42 views
Skip to first unread message

Love U Ruby

unread,
Aug 27, 2013, 6:20:07 AM8/27/13
to rubyonra...@googlegroups.com
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)

--
Posted via http://www.ruby-forum.com/.

jsnark

unread,
Aug 27, 2013, 8:38:42 AM8/27/13
to rubyonra...@googlegroups.com
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 `<main>'

Dheeraj Kumar

unread,
Aug 27, 2013, 9:59:40 AM8/27/13
to rubyonra...@googlegroups.com
NAN is namespaced under Float. So you can only access it through Float::NAN.

-- 
Dheeraj Kumar

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/32254486-51ae-438a-9258-d915ea135986%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Love U Ruby

unread,
Aug 27, 2013, 10:34:30 AM8/27/13
to rubyonra...@googlegroups.com
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..

Dheeraj Kumar

unread,
Aug 27, 2013, 10:38:10 AM8/27/13
to rubyonra...@googlegroups.com
Float::NAN is a constant which holds a special float value `NaN`

-- 
Dheeraj Kumar

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Love U Ruby

unread,
Aug 27, 2013, 11:15:43 AM8/27/13
to rubyonra...@googlegroups.com
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? :)

Dheeraj Kumar

unread,
Aug 27, 2013, 11:18:46 AM8/27/13
to rubyonra...@googlegroups.com
Can you post the code you're confused with? 

-- 
Dheeraj Kumar

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Love U Ruby

unread,
Aug 27, 2013, 11:36:59 AM8/27/13
to rubyonra...@googlegroups.com
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>'

Dheeraj Kumar

unread,
Aug 27, 2013, 11:39:35 AM8/27/13
to rubyonra...@googlegroups.com
You spelt it wrong.

instance_of?, not instace_of?

-- 
Dheeraj Kumar

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Colin Law

unread,
Aug 27, 2013, 11:55:39 AM8/27/13
to rubyonra...@googlegroups.com
On 27 August 2013 15:34, Love U Ruby <li...@ruby-forum.com> wrote:
> 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..

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

Love U Ruby

unread,
Aug 27, 2013, 12:14:23 PM8/27/13
to rubyonra...@googlegroups.com
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 ?

Dheeraj Kumar

unread,
Aug 27, 2013, 12:17:13 PM8/27/13
to rubyonra...@googlegroups.com
Float::NAN contains a special float value NaN.

-- 
Dheeraj Kumar

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages