# Ruby annoyances for a newb

**URL:** <https://www.ruby-forum.com/t/ruby-annoyances-for-a-newb/130032>\
**Category:** Ruby\
**Created:** [February 24, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/ruby-annoyances-for-a-newb/130032 "2008-02-24T17:25:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmiguelf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6a8cbe/32.png) [@fmiguelf](https://www.ruby-forum.com/u/fmiguelf)\
**Post date:** [February 24, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/ruby-annoyances-for-a-newb/130032/1 "2008-02-24T17:25:11Z")

</div>

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 )

- 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 dont’t know wich exceptions they throw!

I would like to know your opinion about these two matters. Probably  
these are not problems at all, only my java side confusing me! =D

Hope I was able to express myself correctly

---

<div class="post-metadata">

**Author:** ![fmiguelf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6a8cbe/32.png) [@fmiguelf](https://www.ruby-forum.com/u/fmiguelf)\
**Post date:** [February 24, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/ruby-annoyances-for-a-newb/130032/2 "2008-02-24T19:09:45Z")

</div>

On Sun, Feb 24, 2008 at 8:25 AM, Miguel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![fmiguelf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6a8cbe/32.png) [@fmiguelf](https://www.ruby-forum.com/u/fmiguelf)\
**Post date:** [February 24, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/ruby-annoyances-for-a-newb/130032/3 "2008-02-24T22:50:43Z")

</div>

On 24.02.2008 17:25, Miguel F. wrote:

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

No, you’re not. Please read about “duck typing”. If you do not want a  
dynamically typed language then clearly Ruby is not for you and you are  
better served with Java. If you want to program in Ruby, you need to  
adjust your mindset. You cannot expect that Ruby is just another Java  
even though some things look familiar.

> - 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 dont’t know wich exceptions they throw!

This is only partly true: even in Java there is a ton of exceptions that  
nobody forces you to catch. There has been a lot of debate about  
checked vs. unchecked exceptions in general. I guess by now everything  
is said about it elsewhere. 🙂 There are pros and cons to both  
approaches. IMHO unchecked exceptions fit Ruby very well while Java’s  
model fits Java very well.

> I would like to know your opinion about these two matters. Probably  
> these are not problems at all, only my java side confusing me! =D

Probably. I suggest you get accustomed to the Ruby way, revisit your  
criticism after a while and see whether you still think it’s valid.

> Hope I was able to express myself correctly

Absolutely. If you are interested, these topics have been discussed  
excessively here so you will find plenty material in the archives.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![fmiguelf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/6a8cbe/32.png) [@fmiguelf](https://www.ruby-forum.com/u/fmiguelf)\
**Post date:** [February 25, 2008, 10:16am UTC](https://www.ruby-forum.com/t/ruby-annoyances-for-a-newb/130032/4 "2008-02-25T10:16:20Z")

</div>

Miguel,

I’m a little frustrated by the dynamic typing of Ruby as well, but you  
get  
used to it quickly. You might rather be interested in the scala language  
([http://www.scala-lang.org/](http://www.scala-lang.org/)) which keeps many of the useful constructs  
of  
ruby, but uses an even stronger typing system than java does. I’m still  
new  
to it, but I’m not sure it has many of the neat introspection properties  
of  
ruby that let you twist and bend the language.

Also check out the Eclipse ruby plugin, which I just discovered and  
which  
makes the lack of typing easier to work with…
