Christopher Dicely
2/24/2008 6:09:00 PM
On Sun, Feb 24, 2008 at 8:25 AM, Miguel Fonseca <fmiguelf@iol.pt> wrote:
> Hello!
>
> I'm a fresh ruby programmer from Portugal and until now I've been coding
> mostly java programs. I'm posting here to find out what you think about
> two things that annoy me in ruby:
>
> - The fact that variables dont have type! Sure this is andy in several
> situations but it can also be a source of problems with wrong input
> being passed to the methods! Because of this I'me forced to check
> manually for the type in the begining of methods (ex: if
> price.instance_of? Fixnum )
While there may be some cases where you need to check for the class of an object
at the beginning of methods, its generally viewed as a practice to be avoided
unless you've identified a specific need for it. The capacity of Ruby objects
to participate in operations is determined by the methods they support
("duck typing") not their class. For the most part, library methods
should document
what they expect and it is the responsibility of client code to
provide an object that
behaves appropriately.
> - Second fact has to do with exceptions, in contrast to what happens in
> java the catching of exceptions is not forced, so most times I end up
> not coding the catch statement wich leads to a lot of run-time errors,
> also for system functions I don't know wich exceptions they throw!
You should be only rescuing exceptions that you can usefully do
something about. Most core methods should identify what they
raise and when in their documentation, and you can use that to
identify what you could rescue, but if you can't reasonably
deal with an error, you shouldn't rescue it.