Forum: Ruby on Rails Why NaN.class throws NameError ?

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-27 12:20
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)
Db9510c96a4c0f6f88c9aa39ab24211a?d=identicon&s=25 jsnark (Guest)
on 2013-08-27 14:40
(Received via mailing list)
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>'
Ce5e201eca55d4f79d0ead9acc91d544?d=identicon&s=25 Dheeraj Kumar (Guest)
on 2013-08-27 16:01
(Received via mailing list)
NAN is namespaced under Float. So you can only access it through
Float::NAN.

--
Dheeraj Kumar
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-27 16:34
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.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?
Ce5e201eca55d4f79d0ead9acc91d544?d=identicon&s=25 Dheeraj Kumar (Guest)
on 2013-08-27 16:40
(Received via mailing list)
Float::NAN is a constant which holds a special float value `NaN`

--
Dheeraj Kumar
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-27 17:15
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? :)
Ce5e201eca55d4f79d0ead9acc91d544?d=identicon&s=25 Dheeraj Kumar (Guest)
on 2013-08-27 17:20
(Received via mailing list)
Can you post the code you're confused with?

--
Dheeraj Kumar
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-27 17:36
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>'
>>
Ce5e201eca55d4f79d0ead9acc91d544?d=identicon&s=25 Dheeraj Kumar (Guest)
on 2013-08-27 17:41
(Received via mailing list)
You spelt it wrong.

instance_of?, not instace_of?

--
Dheeraj Kumar
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-27 17:57
(Received via mailing list)
On 27 August 2013 15:34, Love U Ruby <lists@ruby-forum.com> 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
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2013-08-27 18:14
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  ? If there is no value assigned to the
constant,then on what instance that `#class` is called?
Ce5e201eca55d4f79d0ead9acc91d544?d=identicon&s=25 Dheeraj Kumar (Guest)
on 2013-08-27 18:18
(Received via mailing list)
Float::NAN contains a special float value NaN.

--
Dheeraj Kumar
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.