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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:08:57+00:00 2026-06-16T07:08:57+00:00

(working in Ruby) First off, I apologize in advance. I’m not a programmer by

  • 0

(working in Ruby)
First off, I apologize in advance. I’m not a programmer by training, I’ve simply come to it by dint of convenience and a changing world. What I needed to do involved programming, and I got tired of waiting around for others to do my programming for me. As such, I’ve missed some basic concepts along the way, and when I ask these questions I sometimes make a fool of myself by consequence.

Let’s say I want to define a species/job relationship in classes. I want to define a superclass “BlackAnt” and have subclasses “Worker” “Fighter” and “Queen”

To me, intuitively, this looks something like this:

class BlackAnt
  @legs = 6
end

class Worker < BlackAnt
  @jaws = 'small'
end

but if I then try

ant1 = Worker.new
puts ant1.legs

I get an error. If I amend class BlackAnt to:

class BlackAnt
  attr_accessor :legs
  @legs = 6
end

ant1.legs returns ‘nil’

I’ve tried the method outlined here: http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

and this allows Worker.legs to return ‘6’, but… alas:

ant1 = Worker.new
Worker.legs => '6'
ant1.legs => 'nil'

At a guess, the values of those parent variables are not being initialized each time a new child is spawned.

I feel I’m being silly about this somehow, and the reply is no doubt going to make me curse the day I discovered caffeine-driven all-nighters. What I need to do is arrange things so that I can create objects like so:

ant1 = Worker.new
ant2 = Queen.new
ant3 = Fighter.new

and have them each acquire the appropriate quantity of legs, along with whatever special characteristics were assigned in the child class. The worker/queen/fighter classifications will be bound by namespaces such that the actual calls will be:

ant1 = AntBlack::Worker.new
ant2 = AntRed::Worker.new
ant3 = AntRed::Queen.new

etc.

I’d like to then be able to check the quantity of legs of an individual ant using:

ant1.legs #=> 6

I may be going around my elbow to get to my thumb. If so, feel free to offer alternate suggestions for ways to achieve the same result, I will greatly appreciate the insights.

///updated re:response///

class AntRed
  attr_accessor :legs
  def initialize
    @legs = 6
  end
end

class Worker < AntRed
  @strength = 1
end

result:

irb(main):009:0> ant1 = Worker.new
#=> #<Worker:0x87616ac @strength=1>
irb(main):010:0> ant1.legs
#=> nil
  • 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-16T07:08:58+00:00Added an answer on June 16, 2026 at 7:08 am

    Define your BlackAnt class as follows:

    class BlackAnt
      attr_accessor :legs
    
      def initialize
        @legs = 6
      end
    end
    

    And your Worker class as follows:

    class Worker < BlackAnt
      attr_accessor :strength
    
      def initialize
        super
    
        @strength = 1
      end
    end
    

    The way you currently define the BlackAnt class, you are storing legs as a class level instance variable.

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

Sidebar

Related Questions

I'm working with a Ruby project for school, and have sadly not been able
I have the simple following code, which is working in a ruby (not rails)
Today I started working on my first Ruby project. The goal is to convert
I'm working on my first Ruby on Rails aplication and it's quite big (at
I'm doing my first Ruby on Rails project. I am working on a project
I'm learning Ruby and working on my first real project. The program will receive
This is my first project using Ruby on Rails and I'm working on the
I'm an advanced beginner/intermediate Ruby programmer. I'm really working on improving my Ruby skills,
I am very new to ruby on rails.. working on my first project... am
I'm not a ruby person and i'm working on a project with Ember.js and

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.