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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:31:54+00:00 2026-06-15T18:31:54+00:00

To my current knowledge Ruby classes defined with Class.new should not differ from classes

  • 0

To my current knowledge Ruby classes defined with Class.new should not differ from classes created with the class keyword. Then why do following classes B and C behave differently?

class A
  TEST = 'A'   
  def test
    TEST
  end   
end

class B < A
 def test
  TEST
 end    
end

C = Class.new(A) {        
 def test
  TEST
 end    
}

puts 'A: ' + A.new.test # => "A: A"
puts 'B: ' + B.new.test # => "B: A"
puts 'C: ' + C.new.test # => uninitialized constant TEST (NameError)

Tested with ruby 1.9.3p327 and ruby 1.8.7p358.

  • 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-06-15T18:31:55+00:00Added an answer on June 15, 2026 at 6:31 pm

    cause you a using a block for defining C class, and the scope of that block is the main instead of A

    to have it work:

    C = Class.new(A)
    
    def C.test
      TEST
    end    
    

    or:

    class << C
        def test
          TEST
        end    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a template class of dynamic matrices. With my current knowledge
current code (not working): /^script\s*type=\text\/javascript/i.test(tagName)
Current situation: Ubuntu 8.04 server edition (live server) Postgresql 8.3.7 (from standard repositories) Postgis
My current knowledge: If you are trying to write text files in vbscript /
I have a current need to greatly increase my knowledge around DSLs. Who are
Can you elaborate on the current state of blocks (in the Ruby sense) in
My current knowledge: Oracle does offer index-organized tables and defaults to heap-organized. I heard
My current homework assignment has me creating an iterator class for a list. I'm
My regular expression knowledge is lacking and I'm trying to solve the following issue.
The question regards .Net specifically. Suppose a base class A has 2 derived classes

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.