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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:13:48+00:00 2026-05-27T18:13:48+00:00

Have been observing some unexpected behavior when a class region appears in another class

  • 0

Have been observing some unexpected behavior when a class region appears in another class definition:

Such region overrides whole class definition there.

However, the same code will just add new method to this class, as shown in codes below.

Why is Ruby designed like this, if it isn’t a little beyond intuition?

require 'pp'

def pp_methods obj
  pp obj.methods - nil.methods
end

#first definition of A
class A
  def method1
  end
end

#add method3 to A
class A
  def method3
  end
end

class B
  #class A got overridden within class B
  class A
    def method2
    end
  end
  def initialize
    a=A.new
    pp_methods a  # => [:method2]
  end
end

a1=A.new
pp_methods a1     # => [:method1,:method3]

b=B.new

a2=A.new
pp_methods a1     # => [:method1,:method3]


pp_methods a2     # => [:method1,:method3] 
  • 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-27T18:13:49+00:00Added an answer on May 27, 2026 at 6:13 pm

    Being able to “reopen” classes is actually very useful. By having this ability we can easily add functionality to existing libraries, or fix issues (“monkey-patch”), without having to modify the actual library code. A contrived example:

    class String
      def foo
        "bar!"
      end
    end
    
    "hello".foo # => "bar!"
    

    As for the second case, that A is actually B::A, which is entirely different than A, and thus not reopening your initial A. We can see this here:

    class A
    end
    
    A # => A
    
    class B
    end
    
    B    # => B
    B::A # warning: toplevel constant A referenced by B::A
         # => A
    
    class B
      class A
      end
    end
    
    B::A           # => B::A
    A.object_id    # => 70359576062780
    B::A.object_id # => 70359575976320
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been trying the whole day long and googled the **** out of the
I have been trying to follow some WCF Data Services examples and have the
Have been playing around with linq but there is one thing I cant seem
I have been playing around with some debugging and wrote some C code that
have been playing with the ManagementEventWatcher class and am curious what system events and
Have been digging around the docs but cannot find if there is a function
I have been trying this for the whole day and I managed to make
Have been writing the shell script such as : #! /bin/bash `sqlplus -s <username>/<passwd>@dbname`
Have been looking at the MVC storefront and see that IQueryable is returned from
Have been studying the file system related classes of Adobe AIR 1.5, but so

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.