Stack Level Too Deep?

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:

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
 
S

Sebastian Hungerecker

Ryan said:
=C2=A0 =C2=A0 def remove()
=C2=A0 =C2=A0 =C2=A0 self.remove() ### =3D> Stack Level Too Deep.
=C2=A0 =C2=A0 end

What do you think happens when this method is called?
When remove is called, the above code will execute, which will call remove=
=20
again, which will execute the code again, which will call remove again, whi=
ch=20
will... stack level too deep.

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
R

Ryan Lewis

Sebastian said:
What do you think happens when this method is called?
When remove is called, the above code will execute, which will call
remove
again, which will execute the code again, which will call remove again,
which
will... stack level too deep.

HTH,
Sebastian

Yeah, but I've been trying to run the remove under self<<, but you made
me realise i dont even need to do that =p
 
K

Ken Bloom

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:

Code:
class User < Struct.new(:username, :password)[/QUOTE]
[QUOTE]
def remove()
self.remove() ### => Stack Level Too Deep.
end
end[/QUOTE]

The remove() method just keeps calling itself. I think you wanted
def remove()
self.class.remove(self.username)
end

--Ken
 
J

Julian Leviston

Or maybe super()?

Julian



Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)
VIDEO #3 out NOW!
http://sensei.zenunit.com/



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:

Code:
class User < Struct.new(:username, :password)[/QUOTE]
[QUOTE]
def remove()
self.remove() ### => Stack Level Too Deep.
end
end[/QUOTE]

The remove() method just keeps calling itself. I think you wanted
def remove()
self.class.remove(self.username)
end

--Ken
[/QUOTE]
 
R

Ryan Lewis

Ken said:
The remove() method just keeps calling itself. I think you wanted
def remove()
self.class.remove(self.username)
end

--Ken

Ahh thanks alot! That's exactly what I was looking for.
 
R

Ryan Lewis

Here's another question, how would I go about setting the User class to
output the @@user_hash? I know theres a method for that, i just can't
seem to find it.
 
K

Ken Bloom

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:

Code:
class User < Struct.new(:username, :password)[/QUOTE]
[QUOTE]
def remove()
self.remove() ### => Stack Level Too Deep.
end
end[/QUOTE]

The remove() method just keeps calling itself. I think you wanted
def remove()
self.class.remove(self.username)
end[/QUOTE]

Or maybe super()?[/QUOTE]

You'll get a NoMethodError if you call super(), because you're not
overriding an existing remove method

--Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top