Stack Level Too Deep?

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
[/code]

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..?"
[/code]

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

···

--
Posted via http://www.ruby-forum.com/.

Ryan Lewis wrote:

def remove\(\)
  self\.remove\(\) \#\#\# =&gt; Stack Level Too Deep\.
end

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

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

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

--Ken

···

On Thu, 10 Apr 2008 18:00:15 -0500, Ryan Lewis wrote:

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)

    def remove()
      self.remove() ### => Stack Level Too Deep.
    end
  end

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Sebastian Hungerecker wrote:

Ryan Lewis wrote:

    def remove()
      self.remove() ### => Stack Level Too Deep.
    end

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

···

--
Posted via http://www.ruby-forum.com/\.

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/

···

On 11/04/2008, at 10:10 AM, Ken Bloom wrote:

On Thu, 10 Apr 2008 18:00:15 -0500, Ryan Lewis wrote:

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)

   def remove()
     self.remove() ### => Stack Level Too Deep.
   end
end

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

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Ken Bloom wrote:

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.

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

--
Posted via http://www.ruby-forum.com/.

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

--Ken

···

On Thu, 10 Apr 2008 19:21:32 -0500, Julian Leviston wrote:

On 11/04/2008, at 10:10 AM, Ken Bloom wrote:

On Thu, 10 Apr 2008 18:00:15 -0500, Ryan Lewis wrote:

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)

   def remove()
     self.remove() ### => Stack Level Too Deep.
   end
end

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

Or maybe super()?

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/