Can't figure out how to delete from the hash @file


#1

I'm trying to create a method "deletes" that takes a filename & deletes the key/value pair from the hash @files. How would I accomplish this? Here is my code that doesn't work:

...
class Computer
@@users = {}

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

    @@users[username] = password
end

def create(filename)
    time = Time.now
    @files[filename] = time
    puts "Created: #{filename} @ #{time}."
end

def deletes(filename)
    time = Time.now
    delete_if { |k, v| k == filename }
    puts "#{filename} deleted @ #{time}."
end

def Computer.get_users
    @@users
end

end

my_computer = Computer.new("pbrennan", "321")
my_computer.create("sample-file.rb")
my_computer.create("sample-file2.rb")
my_computer.deletes("sample-file.rb")
puts @files

...


#2

You want to call delete_if on an object (in your case the hash).


#3

Please read my code - I did that. Not working.


#4

On which object did you call delete_if? I looked but I only see the method by itself, no object with it.


#5

Ah yes, I needed the @files.delete_if instead of just delete_if method by itself. Thanks!