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 3759782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:22:11+00:00 2026-05-19T10:22:11+00:00

disclaimer: Code taken from the ruby koans This is from a discussion of constants

  • 0

disclaimer: Code taken from the ruby koans

This is from a discussion of constants scoping within classes. Here is the defintion of a couple few classes:

class Animal
  LEGS = 4
  def legs_in_animal
    LEGS
  end
end

class MyAnimals
  LEGS = 2

  class Bird < Animal
    def legs_in_bird
      LEGS
    end
  end
end

At this point doing MyAnimals::Bird.new.legs_in_bird results in 2 and I understand why–search lexical space for the constant before the inheritance heirarchy.

Then this class is defined:

class MyAnimals::Oyster < Animal
  def legs_in_oyster
    LEGS
  end
end

The tutorial says that now calling MyAnimals::Oyster.new.legs_in_oyster results in 4 and I can’t figure it out. It appears to me that Oyster is a nested class in MyAnimals and as such I expected it to behave the same ways as the Birds class did above. I’m missing some key information about what declaring the class Oyster with explicit scoping means.

can anyone explain this to me? I’ve found hundreds of ruby class tutorials via Google but none of them address this situation.

thank you in advance…

  • 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-19T10:22:12+00:00Added an answer on May 19, 2026 at 10:22 am

    I think this example explains it best. Ruby searches for the constant definition in this order:

    1. The enclosing scope
    2. Any outer scopes (repeat until top level is reached) Any outer scopes (up to but not including the top level
    3. Included modules
    4. Superclass(es)
    5. Top level
    6. Object
    7. Kernel

    EDIT

    Thanks to Mark Amery for pointing out this error. The top-level is only reached in the case where there are no enclosing scopes and/or superclasses. The linked example actually makes this clear, sadly I read it wrong.

    An example for this case:

    FOO = 'I pity the foo!'
    
    module One
      FOO = 'one'
    
      class Two
        FOO = 'two'
    
        def self.foo
          FOO
        end
      end
    
      class Three < Two
        def self.foo
          FOO
        end
      end
    end
    
    class Four
      class Five < Four
        def self.foo
          FOO
        end
      end
    end
    
    describe FOO do
      it "depends where it is defined" do
        expect(FOO).to eq 'I pity the foo!' # top-level
        expect(One::FOO).to eq 'one' # module
        expect(One::Two.foo).to eq 'two' # class
        expect(One::Three.foo).to eq 'one' # outer scope (One) comes before superclass
        expect(Four::Five.foo).to eq 'I pity the foo!' # top-level
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer This question is a repost. I originally asked it here . While there
DISCLAIMER: The following code is not something I would ever use in a real
Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer This is not strictly a programming question, but most programmers soon or later
Full disclaimer: I'm a CS student, and this question is related to a recently
Disclaimer: the question is completely different from Inheritance instead of typedef and I could
Disclaimer, I not do anything in particular with regards this question, just curious. Is
(Disclaimer: I am very new to rails) This plugin looks like it will be
Refreshing on floating points (also PDF ), IEEE-754 and taking part in this discussion

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.