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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:18:51+00:00 2026-06-07T16:18:51+00:00

I don’t understand why this works. module Base attr_reader :first def setup @first =

  • 0

I don’t understand why this works.

module Base
  attr_reader :first
  def setup
    @first = 1
  end
end

module Addon
  attr_reader :second
  def setup
    #super
    @second = 2
  end
end

class Test
  include Base
  include Addon

  def initialize(num)
    @num = num
    setup
  end
end

a = Test.new(1)
p a.first
p a.second

Basically I have a “base” module, which sets up some stuff. I also have an addon module, which sets up some more stuff if some class wants to include it.

Now when I test it, if I don’t have that super call, I get

nil
2

When I do have the super call, I get

1
2

What does super actually do here? It’s calling the setup method from the Base module, even when Base and Addon are not related.

This is the first time I’ve used super in the context of modules. Previously I’ve always used super calls with classes and thought it was just going up the inheritance tree to find a parent with the same method.

Is including multiple modules also setting up some sort of inheritance tree?

EDIT: for context, the Addon module will never be included without the Base module, and the Base module will always be included before any additional Addon modules.

  • 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-07T16:18:53+00:00Added an answer on June 7, 2026 at 4:18 pm

    Yes, when you include modules, they are injected into inheritance chain

    Test.ancestors # => [Test, Addon, Base, Object, Kernel, BasicObject]
    

    Addon module, when included, redefines existing setup method from Base. If you want Base‘s version be called, use super.

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

Sidebar

Related Questions

Don't think that I'm mad, I understand how php works! That being said. I
I don't know: if this works. if it's a good idea. what it is
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't quite understand why this copy constructor is not invoked when I build with
Don't know how to frase this but I found this code wich works as
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I don't know why, but this code worked for me a month ago... maybe
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question

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.