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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:46:10+00:00 2026-05-19T22:46:10+00:00

In Ruby a class named Foo would be defined with class Foo , used

  • 0

In Ruby a class named Foo would be defined with class Foo, used via require 'foo' and would live in $:[0]/foo.rb or something like that.

But what about Foo::Bar? Would it be called with require 'foo/bar'? Would it live in $:[0]/foo/bar.rb? And how would it be defined?

I am very used to Perl, where for personal projects I would make nested classes like Project::User, Project::Text::Index, Project::Text::Search, etc. I would then make a file like Project/Text/Index.pm, which would start with package Project::Text::Index, and be called via use Project::Text::Index;.

Now I’m starting a project in Ruby and have no idea how to do this. For some reason none of the Ruby books or docs I’ve read mention perl-style hierarchical class naming. When they mention inheritance it’s usually via a trivial made up example like class Foo < Bar which doesn’t really help me. Yet I figure it must be possible to do what I’m attempting because Rails (just to take one example) has classes like ActionView::Helpers::ActiveModelFormBuilder.

  • 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-19T22:46:11+00:00Added an answer on May 19, 2026 at 10:46 pm

    You’re combining a couple of concepts here which aren’t really related, namely the load path, inheritance, and the scope resolution operator.

    When requiring (or loading) files the argument to the require keyword is simply taken as a file path and appended to the load search path (the .rb extension is optional for require). Inheritance and nesting don’t come into play here and any file can define anything it wants, e.g.:

    require 'foo' # Looks for "foo.rb" in each of $:
    require 'foo/bar' # Looks for "foo/bar.rb" in each of $:
    

    Nested classes (and modules, variables, etc) are defined as expected but resolved with the scope resolution operator, e.g.:

    class Foo
      def foo; 'foo'; end
      class Bar
        def bar; 'bar'; end
      end
    end
    Foo.new.foo # => "foo"
    Foo::Bar.new.bar # => "bar"
    

    Note that class nesting and inheritance are irrelevant to the location of the file from which they are loaded. There don’t seem to be any explicit conventions for class/module structuring, so you’re free to do what works for you. The Ruby Language page of the Programming Ruby book might be helpful too.

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

Sidebar

Related Questions

In Ruby, is there a way to do something like class Foo attr_reader :var_name
i have a basic ruby class: class LogEntry end and what i would like
my ruby (on rails) class looks like: class Foo def self.method1 someAction end def
How is it that Ruby allows a class access methods outside of the class
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
Is the singleton class in Ruby a class in and of itself? Is it
In the near future, I may be teaching a Ruby programming class to undergraduates
I am trying to add the following method to the Math class in Ruby
Ruby can add methods to the Number class and other core types to get
I was kidding with metaprogramming in Ruby and I did this code: class Class

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.