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:

  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…

  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

Ryan L. 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

Sebastian H. wrote:

Ryan L. 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

On Thu, 10 Apr 2008 18:00:15 -0500, Ryan L. 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

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/

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.

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

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()?

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

–Ken

Ken B. 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.