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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:36:14+00:00 2026-06-17T14:36:14+00:00

I am trying to learn Ruby by reading tutorials on Class Variables. The code

  • 0

I am trying to learn Ruby by reading tutorials on Class Variables.

The code creates a “Worker” object, a class variable is created to keep track of which instance of Worker was created by the user last.

I have copied the code from the author but I get the error:

undefined method `latest' for Worker:Class (NoMethodError)

The code I have found is:

class Worker
  attr_writer :number_of_jobs

  def initialize(name, job)
    @name = name
    @job = job

    @@latest = @name
    @@job = @job
    puts "Lets get started"
  end

  def new_job(job)
    @job = job
    puts "I moved to #{job}!"
    self.fetch_info
  end

  def name_update(name_new)
    @name = name_new
    puts "My new name is #{name_new}."
    self.fetch_info
  end

  def fetch_info
    puts "I'm #{@name} in #{@location}."
  end

  def job_score
    return "#{@number_of_jobs * 10}Fg"
  end


  protected
  def are_you_worker?(guest_name)
    puts "Yes #{guest_name}, I am a worker!"
    return true
  end

  private
  def text_a_message(message)
    puts message
  end

  public
  def tell_friend(where)
    text_a_message("I just applied to #{where}")
  end
end

#running the code
Worker1 = Worker.new("Steve", "Support")
Worker2 = Worker.new("Alan", "PA")
puts Worker.latest

Can anybody see why?

  • 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-17T14:36:15+00:00Added an answer on June 17, 2026 at 2:36 pm

    The Class variables are private inside this class which is causing a problem. Therefore accessing the Worker.latest variable will cause an error as it isn’t visible from instances outside of the class (but it is created and set).

    Additionally, attributes are part of the object not the class so you shouldn’t have an attribute for this class . In native Ruby the class variables are not accessible from outside EXCEPT through a class method (there are extensions in Rails for them tho).

    Hope that helps

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

Sidebar

Related Questions

Trying to learn Ruby, and in a tutorial, I came across this code on
I'm new to Ruby on Rails and trying to learn the framework by reading
I'm trying to learn RegEx in Ruby, based on what I'm reading in The
I am trying to learn Ruby and I have the following code a[0]={ :artist
I'm trying to learn Ruby as well as Ruby on Rails right now. I'm
I am trying to learn Ruby on Rails using this Rails Tutorial. Earlier, I
Hello I'm trying to learn ruby blocks. But I have a trouble to get
I am trying to learn Ruby on Rails by playing around with stuff and
I am trying to learn Ruby on Rails and trying to write some of
I am trying to learn ruby and learn how to handle a while request

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.