I may be doing this wrong. In some languages, I
might do something like:
聽聽user = User.new(name, password)
If the User class is unable to initialize a corresponding
user, it might not produce a valid User object.
What is a good way to do something like this? It seems that I could
do it by having User#initialize throw an exception, but my intuition
is that "user got password wrong" is not exactly an exceptional circumstance;
I have been told exceptions should be used only for unusual cases, and
should not be part of the fairly normal usage of a program.
My assumption that a "User" should be one who is logged in may not
be strictly necessary, but I rather like the way it simplifies everything
else -- this way, only one or two lines of code ever have to consider
the possibility of a user who didn't authenticate successfully.
So far as I can tell, though, the initialize method's return is
politely ignored. So. I perceive 4 options:
1. Throw an exception from User#initialize if the name and password
are invalid.
2. Add some kind of check immediately after this call which
verifies a successful login, and otherwise sets the local variable
user to nil.
3. Add a User#valid method or something similar, and check it all
over.
4. Ask ruby-talk, because surely I'm missing something obvious.
(Sorry about the stupid newbie questions; I promise to ask smart questions
once I've gotten better at this.)
-s