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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:16:27+00:00 2026-06-02T22:16:27+00:00

Newbie here, having a hard time understanding Class methods and why I cannot get

  • 0

Newbie here, having a hard time understanding Class methods and why I cannot get an attribute to show up correctly in the instance.

class Animal
  attr_accessor :noise, :color, :legs, :arms

  def self.create_with_attributes(noise, color)
    animal = self.new(noise)
    @noise = noise
    @color = color
    return animal
  end

  def initialize(noise, legs=4, arms=0)
    @noise = noise
    @legs = legs
    @arms = arms
    puts "----A new animal has been instantiated.----"
  end
end

animal1 = Animal.new("Moo!", 4, 0)
puts animal1.noise
animal1.color = "black"
puts animal1.color
puts animal1.legs
puts animal1.arms
puts

animal2 = Animal.create_with_attributes("Quack", "white")
puts animal2.noise
puts animal2.color

When I use the class method create_with_attributes (on animal.2), I expect "white" to appear when I puts animal2.color.

It seems as though I have defined it using attr_accessor just like I have “noise”, and yet noise appears correctly while color will not. I do not get an error when I run this program, but the .color attribute is just not appearing. I believe it is because I have somehow labeled it incorrectly in the code.

  • 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-02T22:16:28+00:00Added an answer on June 2, 2026 at 10:16 pm

    self.create_with_attributes is a class method, so setting @noise and @color within it is not setting an instance variable, but instead what’s known as a class instance variable.

    What you want to do is set the variables on the instance you’ve just created, so instead, change self.create_with_attributes to look something like:

     def self.create_with_attributes(noise, color)
         animal = self.new(noise)
         animal.noise = noise
         animal.color = color
         animal
     end
    

    which will set the attributes on your new instance, instead of on the class itself.

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

Sidebar

Related Questions

SQL Newbie here, and I'm having a hell of a time finding what should
Android newbie here. I'm using this tutorial to show some rows that I fetch
I'm a beginner in C# and having hard times understanding Events in C# ..
Newbie here programming my first app (having made several tutorial apps). I am using
Java newbie here, I'm having trouble setting a new line in this code: String
I'm a C# newbie who's having problems understanding why his first attempt at understanding
Greetings all, newbie to Rails here. I'm currently having issues routing /profile to the
git newbie here, bear with me if this is trivial. I cannot find this
Total newbie here, regarding sqlite, so don't flame too hard :) I have a
I am a newbie to Git and having trouble understanding how to use Git.

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.