Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:58:09+00:00 2026-05-26T14:58:09+00:00

NilClass , TrueClass and FalseClass having one instance each, namely nil , true and

  • 0

NilClass, TrueClass and FalseClass having one instance each, namely nil, true and false, which are constants, what is the purpose of having these classes? Why cannot they be instances of the Object class, and all the relevant methods be simply defined as singleton methods on nil, true and false? A related question is, why are these not defined as constants?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:58:10+00:00Added an answer on May 26, 2026 at 2:58 pm

    It keeps with the idea that “everything is an object” and “objects are specialized by the classes they are instances of”.

    nil, true, and false are all objects (and are thus instantiations of a class with methods). The imposition that they are 1) the sole inhabitants of the respective type and are 2) immutable objects allows for implementation optimizations — and really, isn’t one nil enough?

    A helpful error message without specialization for the values: x.class “just works”.

    > > nil.foo
    > => #<NoMethodError: undefined method `foo' for nil:NilClass>
    

    I am glad it said NilClass 🙂

    This class-instance approach also makes re-opening NilClass — for better or worse — as easy and consistent with how it might be done for other types.

    At least as of Ruby 1.9.2 it is not possible to re-assign true, false or nil (Python 2.x allowed re-assignment of True/False, but does not in Python 3.x). Note that because true/false/nil are not constants they can be optimized into the AST — or whatever the implementation uses — as “literal values” without a constant look-up.

    > > VERSION
    > => "1.9.2"
    > > true = false­
    > => #<SyntaxError: Can't change the value of true>
    > > [].each {|tru­e|}
    > => #<SyntaxError: Can't change the value of true>
    

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

undefined method `<' for nil:NilClass the line the error occurs on @object.expires_at < Date.today
My app seems to randomly be throwing a undefined method `map' for nil:NilClass error
I am having an issue with the below code in one of my views:
I get the following error: undefined method `[]' for nil:NilClass The related snippet in
I am getting an error: Undefined method build for nil:NilClass when trying to build
I get this error: undefined method 'vote_sum' for nil:NilClass for this line in my
I wonder howto get from a string nil the nil:NilClass? nil.inspect -> nil I
I am getting NoMethodError in SitesController#index undefined method `subdomain' for nil:NilClass I have an
I'm getting this strange error using Rails 3.0.2. ActionView::Template::Error (undefined method `parent' for nil:NilClass):
How does one remove trailing character correctly in the following sentence if it's in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.