Newbie needs help: Trying to implement a "update" Method in a self made class

I know this code looks like a lot, but i am stuck here.

I want to be able to update a filename and also see, if it even already exists, but i cant get it to work…

(i am learning Ruby on codecadamy and am a completely beginner, it would be nice if you could keep the suggested syntax as simple as possible :D)


(I am also not a native speaker, pls ask if smth is unclear to you)

Thanks in advance to everyone that helps :smiley:

Tip: you can post code enclosed by three backticks.

You say ‘filename’, but it looks like you are trying to use a Hash. class Hash - RDoc Documentation

A Hash contains several key → value pairs. You can check if a key is present using has_key? and remove a key using delete.

In your update method, your if test checks if the filename is present. If it is not present, you add the filename as a new key. If it is not present, you ask for a new filename and then add that as a new key. In either case, a new filename key gets added to the hash.

Maybe in the else branch of your update method you need to delete the “filename2” key? But I’m not sure what you mean by ‘update’.

$ irb
2.7.1 :001 > files = {}
2.7.1 :002 > files['one.txt'] = Time.now
2.7.1 :003 > puts files
{"one.txt"=>2020-05-03 00:34:50.915230344 +0100}
 => nil 
2.7.1 :004 > files['two.txt'] = Time.now
2.7.1 :005 > puts files
{"one.txt"=>2020-05-03 00:34:50.915230344 +0100, "two.txt"=>2020-05-03 00:35:01.192976411 +0100}
 => nil 
2.7.1 :006 > files.delete('one.txt')
 => 2020-05-03 00:34:50.915230344 +0100 
2.7.1 :007 > puts files
{"two.txt"=>2020-05-03 00:35:01.192976411 +0100}
 => nil 
2.7.1 :008 > files.has_key?('one.txt')
 => false 
2.7.1 :009 > files.has_key?('two.txt')
 => true

Ah i can see why that is confusing now. I wanted to be able to update the hash.
what i mean by that is: i want to be able to call the hash and see if it is already there.

if it isnt a new file should be generated rather than just getting notified, that there isnt such a file with that name yet.

if there is a file though, i intended the update method to be something like a rename (updating or changind the name) method. so if it finds a file that has the called name, i would like to be able to change it and save it to the hash as the newer version, so if i would call the method it would show up as the newest version
as if you would rename a file on your computer almost.

one of the problems is though, that ruby doesn’t let me type in the changed name, if it finds a file…

i want to be able to call the hash and see if it is already there.

You have already done that - you call @files[filename2].nil? to see that filename2 is not present. (This is fine, although I suggest you use @files.has_key?(filename2) instead - you will have to swap the then/else blocks over.)

if it isnt a new file should be generated rather than just getting notified,

Your code is adding the new file, filename2, to the hash @files.

so if it finds a file that has the called name, i would like to be able to change it and save it to the hash as the newer version

The only thing missing in your current code is to delete the existing filename as a key in the hash - your code results in both the old and new filenames in your hash. You can do that by adding the line @files.delete(filename2) somewhere in the else branch.

Try adding puts @files in your code, to see how the hash @files is changing.

Thank you very much, i will try that directly and report back asap :smiley:

I’ve tried it out now and that actually fixed my problem! Thank you very much.
The only other thing i would like to ask now is: is it not possible for me, to access @files out of the class? like when i ended it and want to puts @files onto the console afterwards, that doesn’t work and i am not quite sure why. is it because it is an instance object and therefore only accessable within the class itself? or am i confusing things now?

Thank you again very much for your help :smiley:

is it not possible for me, to access @files out of the class? like when i ended it and want to puts @files onto the console afterwards, that doesn’t work and i am not quite sure why. is it because it is an instance object and therefore only accessable within the class itself?

You are right. One of the points of object-oriented programming is to encapsulate fields and methods inside a class, and the class must provide access to anything it allows externals to access.

What you need to do is provide an “accessor” to your @files variable.

The straight-forward way to do this is to write a method called, e.g., “files” which will return the value of @files:

class Computer
  # ALL YOUR OTHER CODE

  def files
    @files
  end
end

But more Ruby-like is to use an attr_accessor:

class Computer
  attr_accessor :files

  # ALL YOUR OTHER CODE
end

either way, you should be able to call my_computer.files at the end to access the files.

Thank you very much again :smiley:
feels so much better to see the code working now xD