Trying to debug this error

#!/usr/bin/env ruby
# frozen_string_literal: true

# Created by Paul A.Gureghian in May 2020. #
# This Ruby program demos the creation of a class and the generation of instances of the class. #

# Start of program. #

# Create the class. #
class Computer
  
  @@users = {}  

  def initialize(username, password)
    
    @username = username
    @password = password
    @@users[username] = password
    @files = {}
    
  end    

  def create(filename)

    time = Time.now
    @files[filename] = time
    puts "#{filename} was created by #{username} at #{time}."

  end  

  def Computer.get_users

    @@users

  end   
end    

# Instantiate the class. #
paul_computer = Computer.new('paul', 'monkey123')

# Call the "create" class method and create a file. #
paul_computer.create('ruby.txt')

# Print out user(s) name(s). #
puts "User(s): #{Computer.get_users}."

# End of program. #

Hi Paul! A Ruby object’s data is private by default. You need to create a getter method to make the data from the Computer object readable. The most efficient way to do that is by using an attr_reader method:

@@users = {}  
  attr_reader :username, :files
  def initialize(username, password)

First I would like to ask you if you know why one of my posts was removed. I posted a link to my github repo. I didn’t see anything in the community guidelines about it. I asked this question in another category here, but nobody replied yet.

The error-message is telling you what the problem is and what to do: you wrote ‘username’ , you should have written ‘@username’.

See the start of the message “Virtual_computer.rb:27 in ‘create’” - just do what it says, go to line 27, you will be in the ‘create’ method, and change ‘username’ to ‘@username’. The ‘@’ is for instance variables, and important in Ruby.

Trust Ruby’s error messages! They are usually very informative.

pcl, do you have any idea why one of my posts was removed ?

I posted a link of one of my githib repos and they said it went against community standards.

There was nothing in the community guidelines about the posting of links.
I asked in another forum tag room, but no reply yet.

As for the error, I used “@username” instead of “username” and it works now.

Any idea why the “initialize” method is never used during the instantiation of the class ?

I use “Computer.new” instead of “Computer.initialize” and it still works.

“new” is a class method on every Ruby class that instantiates an object.
“initialize” is an instance method on every Ruby class that is called as part of instantiating the object using the “new” class method.

Add the following line of code to the top of your “initialize” method:
puts "Calling initialize with #{username}, #{password}"

It should print the value of username and password when you call Computer.new(‘paul’, ‘monkey123’) as follows:

Calling initialize with paul, monkey123

I hope that helps. Let me know if you have other questions. I struggled like you many years ago while learning Ruby at first, so I sympathize. Still, it was a lot of fun to go through the Ruby learning process.

A post of mine was recently removed here, I posted a link to my github repo. they said it was violation of community guidelines. I saw nothing about that in the guidelines. any idea why posting a github link would be a violation ? I asked this question in the “community” tag forum, but no reply yet.

Moderators may remove posts for any reason they deem appropriate, so I couldn’t know for sure.

Still, one of the community guidelines is not to post a link to digital assets that don’t belong to you without permission from its authors. Perhaps you needed to clarify that it was your own repository and not containing any files not originally authored by you.

Hopefully, you’ll get a response soon on why it was removed.

Thanks. guess I’ll have to just copy and paste my code in here.

So, the ‘new’ keyword is ‘mapped’ , ‘linked’ , or otherwise associated with the ‘initialize’ public instance method ? so, just to be clear, initialize is being used under the hood by the code it self ?

In all of the object oriented languages that I know of, classes can have an initializer/constructor method that gets called first when an instance of the class is instantiated. In Ruby, that default initializer method is called initialize. So when you define an initialize method, it gets called first when the class is instantiated. In Ruby, to instantiate a new instance of a class, you simply call the new method on the class name. The arguments that you pass to the new method get passed to the initialize method.

Think I understand now.

To access the metaclass of Computer, you must use self
class Computer
@@users = {}

def initialize(username, password)

@username = username
@password = password
@@users[username] = password
	@files = {}

end

def create(filename)
time = Time.now
@files[filename] = time
puts “#{@filename} was created by #{@username} at #{time}.”
end

def self.get_users
@@users
end

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs