R
Ryan Lewis
I have a User class that handles users as structs and adds them to a
hash within the class. Anyways, I was making a Remove functions for it.
Here's my code:
Alright throw this code at the bottom to test the remove functions...
Now why does it come up with a SystemStackError: stack level too deep on
User["Alpha"].remove?
This problem has kept me up all night O_O
Attachments:
http://www.ruby-forum.com/attachment/1718/user.rb
hash within the class. Anyways, I was making a Remove functions for it.
Here's my code:
Code:
class User < Struct.new(:username, :password)
class <<self
def list_hash
@@list_hash ||= {}
end
def list
list_hash.keys.sort
end
def [](user)
list_hash[user]
end
def []=(user, struct)
list_hash[user] = struct
end
def remove(user)
list_hash.delete(user) ### Works perfectly fine
end
end
def initialize(username, password)
self.username = username
self.password = password
self.class[username] = self ### Same as User[username] = self
end
def remove()
self.remove() ### => Stack Level Too Deep.
end
end
Alright throw this code at the bottom to test the remove functions...
Code:
User.new "Alpha", "Aye"
User.new "Beta", "Bee"
User.new "Charlie", "Sea"
puts User.list, "" ### => ["Alpha", "Beta", "Charlie"]
User.remove("Beta") ### Beta removed..
puts User.list ### => ["Alpha", "Charlie"]
User["Alpha"].remove ### Stack level too deep >_<
puts "Alpha removed..?"
Now why does it come up with a SystemStackError: stack level too deep on
User["Alpha"].remove?
This problem has kept me up all night O_O
Attachments:
http://www.ruby-forum.com/attachment/1718/user.rb