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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:14:40+00:00 2026-06-14T00:14:40+00:00

I have a folder structure like the following in one of my projects: lib

  • 0

I have a folder structure like the following in one of my projects:

  • lib
    • bar.rb
    • bar
      • other_bar.rb
      • another_bar.rb
      • next_bar.rb
      • …

bar.rb

require File.expand_path(File.dirname(__FILE__) + "/bar/other_bar.rb")

class Bar
  puts "running BarBase"
end

bar/other_bar.rb

module Bar
  class OtherBar
    puts "running module Bar with class OtherBar"
  end
end

If I now run ruby bar.rb I get this:

running module Bar with class OtherBar
bar.rb:3:in `’: Bar is not a class (TypeError)

I’d like to have a similar structure to a rails model inheritance structure. How can I fix this? So far as I know ruby does not support this out of the box. Is there a workaround for such a situation?

  • 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-14T00:14:41+00:00Added an answer on June 14, 2026 at 12:14 am

    Bar can’t be a module and a class, they are different things.

    Change bar.rb to module Bar or change other_bar.rb to class Bar.

    Whichever it is, it has to be consistent. You can’t change one to the other. The question is which should it be? If Bar is a container for other classes and only has a few global singleton methods? Then it’s a module. But if it can be instantiated, then it’s a class.

    And yes, you can nest classes. This is totally acceptable:

    class Bar
      class OtherBar
        puts "running module Bar with class OtherBar"
      end
    end
    
    Bar::OtherBar.new # yay!
    

    Modules and Classes can be nested inside either other in any way you see fit.


    Edit with some commented examples to help clear this all up:

    module Foo
    
      # Foo::A
      class A
        # simple namespaced class
      end
    
      # Foo::B, inherits from Foo::A
      class B < A
        # inherting from a class in the same namespace
      end
    
      # modify Foo::B
      class B
        # When modifying an existing class you don't need to define the superclass
        # again. It will raise an error if you reopen a class and define a different
        # superclass. But leaving it off is fine.
      end
    
      # nested module Foo::Inner
      module Inner
    
        # Foo::Inner::C 
        class C
          # simple more deeply namespaced class
        end
    
        # Foo::Inner::D, inherits from Foo::A
        class D < A
          # inherits from a class in a parent namespace
    
          # works because ruby looks upward in the nesting chain to find missing constants.
        end
    
        # Foo::Inner::Foo
        class Foo
          # simple nested class with the same name as something in a parent namespace
    
          # This is a totally different Foo, because it's in a different namespace
        end
    
        # Foo::Inner::E, inherits from Foo::Inner::Foo
        class E < Foo
          # class inhereting from another class in the same namespace
    
          # Foo::Inner::Foo is "closer" than the global Foo, so that gets found as the superclass
        end
    
        # Foo::Inner::F, which mixes in the gloabl module Foo
        class F
          # the :: constant prefix says to start looking in the global namespace
          # so here we include the top level module Foo, and not the "closer" in namespace Foo::Inner::Foo
          include ::Foo
    
          # This is an error. This attempts to include the class Foo::Inner::Foo since thats the closest by namespace
          # thing that matches the constant Foo. (you can't include classes, only modules)
          # You need the :: prefix to grab the global Foo module
          include Foo
        end
    
      end
    end
    
    # Z decalred in the global namespace, which inherits from the deeply nested class Foo::Inner::C
    class Z < Foo::Inner::C
      # Any class anywhere can inherit from any other class in any namespace.
      # Just drill in!
    end
    
    # the following 2 declarations at this point would be identical
    
    # This defines a class deep with in a namespace
    class Foo::Inner::Foo::Bar < Foo::A
    end
    
    # same as above, but reopens each namespace
    module Foo
      module Inner
        class Foo
          class Bar < ::Foo::A
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder structure like this: /some_folder /tmp /tmp/foo /tmp/foo/fu * /tmp/bar /tmp/bar/bah
I have the following folder structure /project /scrape item.py /spiders myscraper.py inside the file
I have a file structure like so /foo/bar/ ├── .foo.cfg ├── foo.cfg ├── foo.data
I have a folder structure like this: /articles .index.php .second.php .third.php .fourth.php If I'm
I have folder structure and I would like to create JSON objects based on
If I have a folder structure that looks like this: / /bin/myComponent.cfc /reports/index.cfm How
My folder structure is like - root admin create_page.php pages my_page1.php my_page2.php I have
I have a files structure like this: Class (folder): - User.php - Rule.php Scripts
Suppose I have the following (desired) folder structure: *CommonProject *Project#1 ----> CommonProject(link) *Project#2 ---->
I have the following folder structure: FolderA --Folder1 --Folder2 --Folder3 ... --Folder99 Folders 1

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.