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

  • Home
  • SEARCH
  • 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 6203573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:56:25+00:00 2026-05-24T04:56:25+00:00

This question is strictly about Struct behavior, so please no why in the wide

  • 0

This question is strictly about Struct behavior, so please no “why in the wide world of sports are you doing it that way?”

This code is INCORRECT, but it should illustrate what I am trying to understand about Ruby Structs:

class Person < Struct.new(:name, :last_name)
end

class ReligiousPerson < Person(:religion)
end

class PoliticalPerson < Person(:political_affiliation)
end

### Main ###

person = Person.new('jackie', 'jack')
pious_person = ReligiousPerson.new('billy', 'bill', 'Zoroastrianism')
political_person = PoliticalPerson.new('frankie', 'frank', 'Connecticut for Lieberman')

As you can see, there’s an attempt to define a class inheritance using Structs. However, Ruby gets cranky when you try to initialize ReligiousPerson or PoliticalPerson, of course. So given this illustrative code, how is it possible to inherit named params using this type of class inheritance using Structs?

  • 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-24T04:56:26+00:00Added an answer on May 24, 2026 at 4:56 am

    You could define new Structs, based in Person:

    class Person < Struct.new(:name, :last_name)
    end
    
    class ReligiousPerson < Struct.new(*Person.members, :religion)  
    end
    
    class PoliticalPerson < Struct.new(*Person.members, :political_affiliation)
    end
    
    ### Main ###
    
    person = Person.new('jackie', 'jack')
    p pious_person = ReligiousPerson.new('billy', 'bill', 'Zoroastrianism')
    p political_person = PoliticalPerson.new('frankie', 'frank', 'Connecticut for Lieberman')
    

    Result:

    #<struct ReligiousPerson name="billy", last_name="bill", religion="Zoroastrianism">
    #<struct PoliticalPerson name="frankie", last_name="frank", political_affiliation="Connecticut for Lieberman">
    

    Immediate after posting my answer I had an idea:

    class Person < Struct.new(:name, :last_name)
      def self.derived_struct( *args )
        Struct.new(*self.members, *args)
      end
    end
    
    class ReligiousPerson < Person.derived_struct(:religion)  
    end
    
    class PoliticalPerson < Person.derived_struct(:political_affiliation)
    end
    
    ### Main ###
    
    person = Person.new('jackie', 'jack')
    p pious_person = ReligiousPerson.new('billy', 'bill', 'Zoroastrianism')
    p political_person = PoliticalPerson.new('frankie', 'frank', 'Connecticut for Lieberman')
    

    Works fine!

    You may also add #derived_struct to Struct:

    class Struct
      def self.derived_struct( *args )
        Struct.new(*self.members, *args)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note that this question is about CGLayer (which you typically use to draw
This is a question about using Visual Studio 2003. Sorry it is not strictly
I know that this is strictly seen not a programming question but since it
This is strictly an opinion/experience question for research purposes. I was wondering what coding
This isn't strictly a programming question, but I'm asking it here because it's certainly
Disclaimer This is not strictly a programming question, but most programmers soon or later
I know this isn't strictly a programming question but y'all must have experienced this.
I admit this is not strictly a programming question, although I do use my
I know this isn't strictly speaking a programming question but something I always hear
Although I found many answers and discussions about this question, I am unable to

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.