1

I am trying to build a very very simple computer module that allows you to create files, view them, and edit them. I am having trouble with undefined methods and the exact nature of instance variables. I will get to those later. You can tl;dr and just go to the bolded areas.

class Computer
@@users = Hash.new
def initialize(username, password)
    @username = username
    @password = password
    @files = Hash.new
    @files["Groceries.txt"] = "10:20"
    @files["Blane.txt"] = "10:30"
    @@users[username] = password
end
def create(filename)
    time = Time.now
    @files[filename] = time
    puts "A new file, #{filename}, was created at #{time}"
end
def Computer.get_users
    return @@users
end
end

So I am just defining my class variable "Computer" here.

puts "You need to login!"
puts "Username?"
userName = gets.chomp.downcase
puts "Password?"
passWord = gets.chomp
my_computer = Computer.new(userName, passWord)
puts "You logged in with Username: #{userName}"
puts "and Password: #{passWord}" 
puts "What would you like to do?"
puts "-- Create a file (type create)"
puts "-- Change a file (type change)"
puts "-- View a file (type view)"
puts "-- Delete a file (type delete)"
answer = gets.chomp.downcase
case answer
when "create" 
    puts "What is the name of the file?"
    @create = gets.chomp.capitalize
    if @files[@create].nil?
    my_computer.create(@create)
    else
        puts "That file already exists!"
    end
when "change"
    puts "What is the name of the file you want to change?"
    @files.each do |x, y|
        puts "#{x}: #{y}"
    end
    @change = gets.chomp.capitalize
end

Now when I get the error "undefined method `[]' for nil:NilClass" which is referring to the line

if @files[@create].nil?

which is just checking if the new file is already in it. I don't know if this is a problem with where I am defining the variables, or if it is that they are instance variables, or what the problem is. Furthermore if I add the line:

my_computer.initialize(userName, passWord)

I get the following error: private method `initialize' called for #

My final problem is that when I try and print out the filenames that exist already with the following code

@files.each do |x, y|
        puts "#{x}: #{y}"
    end

I get the following error: undefined method `each' for nil:NilClass

I thought that the .each method was universal, but now it seems I have to define it in my class?

Sorry for the wall of text, but I'm very new to ruby and this is my first programming language, so I am trying to figure out the nuances of the language. Thanks in advance!

1
  • I like how I'm a file Commented May 30, 2014 at 18:45

1 Answer 1

0
if @files[@create].nil?

and

@files.each do |x, y|
    puts "#{x}: #{y}"
end

This code is placed outside of the Computer class definition. And since @files is a class instance variable (it's defined inside the Class Computer... end block), it's only accessible within the class itself. It's NOT accessible outside of it.

my_computer.initialize(userName, passWord)

You cannot directly call the initialize method. Alternatively, this method is automatically called when you call a new method. So your code:

my_computer = Computer.new(userName, passWord)

Automatically calls initialize on my_computer.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you so much! I was curious about the initialize method. Is initialize a special type of method that automatically activates, or is because when I created my_computer and specified two parameters it defaulted to initialize because it takes two parameters? So for the files, if it is a class variable, should I make it an instance variable? Global? Or some other one?
@user3688362 initialize is actually automatically invoked within the new method, that explains why you are expected to pass the same number of parameters to new as you've declared that initialize takes. Still, don't even think to define a new method :)
Also I thought that the @variable was an instance variable and @@variable was a class variable.
Also if initialize is automatically invoked, is the create method also invoked automatically? Or would it be automatically invoked if I defined a new variable with one parameter, as opposed to two?
@AndrewWeatherly @variable is a class instance variable, That means there's a @files variable within every instance of Computer. However, @@variable is a class variable, that means there's a single @@variable that's shared among all instances of Computer. Still, both of them are only accessible within the class definition.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.