1.7 Instantiation Nation - wrong number of arguments


#1

Here's my code:

class Computer
def initialize(username, password)
@username = username
@password = password
@files = {}
end
@@users = {}
def initialize
@@users[username] = password
end
def create(filename)
time = Time.now
@files[filename] = time
puts "New file #{filename} was created by #{username} at #[time}."
end
def Computer.get_users
return @@users
end
end

my_computer = Computer.new("ScoGro", "1234")

=========
I did fine on all the exercises before this one, but now I'm getting the error "wrong number of arguments (2 for 0) - which seems weird, since the Computer class calls for two arguments.

Help?


#2

You have two initialize methods.


#3

Hm. Indeed I do. But that wasn't a problem in the exercises before this one - the only thing I added this time around was the last line:

my_computer = Computer.new("ScoGro", "1234")

Everything else, in exactly the same order, had been in exercise 1.6 and it passed.


#4

Probably because in the previous exercise there's no instantiation of our class (no object is actually created, we only define the class), so the initialize method is never called and therefore Ruby doesn't know that there's a conflict.

Have you tried removing the initialize method not taking any parameter?


#5

Yes, I did, and that fixed it.

I'm still irked that it worked in the previous exercise, though. :stuck_out_tongue: