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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:25:00+00:00 2026-06-02T20:25:00+00:00

Can you tell me why this isn’t working? The code below outputs student1 &

  • 0

Can you tell me why this isn’t working?

The code below outputs student1 & student2 properly, but I cannot get the class method to work on student3.

All I am trying to do is assign a class method .create_with_species , with the species attribute = to “Human”.

However, when I run the program, I get an error that “local variable or method sex is undefined”. I’m a newbie and I can’t figure out what I’ve done wrong!

It was my understanding that since I had identified “sex” in the Initialize method I should be able to use it within a class method such as create_with_species. I tried explicitly defining sex in the class method, as student.sex = sex, but still got the same error.

class Students

  attr_accessor :sex, :age, :species

  def self.create_with_species(species)
    student = Students.new(sex,age)
    student.species = species
    return student
  end

  def initialize(sex, age)
    @sex = sex
    @age = age
    puts "----A new student has been added----"
  end

end

student1 = Students.new("Male", "21")
puts student1.sex
puts student1.age
puts

student2 = Students.new("Female", "19")
puts student2.sex
puts student2.age

student3 = Students.create_with_species("human")
  • 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-02T20:25:01+00:00Added an answer on June 2, 2026 at 8:25 pm

    sex and age are not defined. Do you mean to use a class variable? Using @age or @sex will result in nils, as no class attributes are set and the instance is not yet created, hence no instance variables set either. You don’t need to state Students.new in create_with_species. new will suffice because you are scoped in Students. I think you would be better off passing a hash object to initialize, and just set the species variable there. It will be nil if not assigned explicitly, or you can have a default value.

    Try this:

    class Students
    
      attr_accessor :sex, :age, :species
    
      def initialize(options)
        @sex = options[:sex]
        @age = options[:age]
        @species = options[:species]
        puts "----A new student has been added----"
      end
    
    end
    
    student1 = Students.new(:sex => "Male", :age => "21")
    puts student1.sex
    puts student1.age
    puts student1.species
    puts
    
    student2 = Students.new(:sex => "Female", :age => "19")
    puts student2.sex
    puts student2.age
    
    student3 = Students.new(
        :sex => "Hermi", :age => "2", :species => "alien"
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me why this isn't working? It always returns false . $str
can anyone tell me why this animation isn't starting? i've tried putting code in
Can anyone tell me why this isn't working? >>> import mock >>> @mock.patch('datetime.date.today') ...
Can someone tell me why this isn't working? I'd like to get the sum
Can anyone tell me why this isn't working for me? Example page here http://totalcommunitycollegemove.com/post-438.html
Can anyone tell me why this isn't working? function changeBG(element_id){ document.getElementById(element_id).className= arrowActive; setTimeout(document.getElementById(+element_id+).className= 'arrow',
Please can someone tell me why this isn't working before I defenestrate everything on
I can't tell if this is a bug or what. The following code works
can anybody tell me why this works <g:each var=n in=${com.pp.News.list()}> <h2>${n.t}</h2> <p>${n.tx}</p> </g:each> but
Can anybody tell me why this function isn't copying the file at all? $pluginfile

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.