Ruby lesson: 5. Getting more creative


#1

https://www.codecademy.com/en/courses/ruby-beginner-en-X5wcR/0/5?curriculum_id=5059f8619189a5000201fbcb

Below is my code for lesson 5. Getting more creative.

It returns: nil (up to this stage), but ... in lesson 6. Who'er the users? it returns:

The filename films.txt was created by Edgar at 2016-08-05 14:04:24 +0000
The filename cars.txt was created by Michael at 2016-08-05 14:04:24 +0000

My code for lesson 5:

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 "The filename #{filename} was created by #{@username} at #{time}"
end

end

Is this because #{filename} and #{@username} are still empty in lesson 5? If yes, should it not at least put:

The filename nil was created by nil at 14:04:24 +0000


#2

Your code prints nothing. Not nil, nothing.
You're not invoking that method, program control never reaches that line.


#3

I don't understand ... I get nil in the console.

Or do you mean that the program never reaches puts statement, so the #{filename} and the #{@username} never get an assignment? And so the puts statement doesn't print?


#4

The class is created and the methods are created. Then your program is done. That is all it does. They are never instantiated/invoked. Your code prints nothing. The text in the console is not from your code, because your code doesn't execute any instruction to do printing.


#5

Thank you, ionatan, for taking the time. That is wonderful. I totally get it now. :relaxed:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.