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’.
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.
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?
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.