# Ruby good-practice

**URL:** <https://www.ruby-forum.com/t/ruby-good-practice/141846>\
**Category:** Ruby\
**Created:** [July 1, 2008, 12:23am UTC](https://www.ruby-forum.com/t/ruby-good-practice/141846 "2008-07-01T00:23:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 1, 2008, 12:23am UTC](https://www.ruby-forum.com/t/ruby-good-practice/141846/1 "2008-07-01T00:23:02Z")

</div>

Hi, I’m fairly new to Ruby (3 weeks) and I always find my code for my  
little projects to be awefully cluttered and excessive. Also, I’m never  
sure when to use what. I’m sure a lot of this comes with practice and  
experience, but it would be nice if someone could give me some general  
pointers for good-practice or link me to a site that already has some  
tips. I don’t want to get too deep into my bad practices and habits. =)

Here’s just a sample off the top of my head:

1. What’s the stance on global variables? When to use them, if at all?
2. Two arrays that have related info. (i.e. a[0] relates to b[0]) or one  
hash?
3. If I’m storing data and know I need to collect statistics on that  
data,  
should I store everything and then iterate through them to capture  
stats. or  
store and collect simultaneously?  
etc…

I guess what I’m looking for is what’s common practice out there in the  
professional world. Sometimes I write 100 lines of code and find someone  
else implemented the same thing in 3 lines. =(

Anyway, anything would help! THANKS!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 1, 2008, 12:33am UTC](https://www.ruby-forum.com/t/ruby-good-practice/141846/2 "2008-07-01T00:33:49Z")

</div>

El Martes, 1 de Julio de 2008, Justin To escribiÃ³:

> 1. What’s the stance on global variables? When to use them, if at all?

AFAIK try not using them (except if they are really needed).

> 1. Two arrays that have related info. (i.e. a[0] relates to b[0]) or one  
> hash?

A Hash, of course (IMHO).

Regards.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 1, 2008, 1:24am UTC](https://www.ruby-forum.com/t/ruby-good-practice/141846/3 "2008-07-01T01:24:10Z")

</div>

Thanks, very helpful. How about using ‘begin…rescue…ensure…end’.  
Should I use this everywhere in my code and for every program I do? Or  
is this only for debugging?

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 1, 2008, 1:46am UTC](https://www.ruby-forum.com/t/ruby-good-practice/141846/4 "2008-07-01T01:46:53Z")

</div>

El Martes, 1 de Julio de 2008, Justin To escribiÃ³:

> Thanks, very helpful. How about using ‘begin…rescue…ensure…end’.  
> Should I use this everywhere in my code and for every program I do? Or  
> is this only for debugging?

Some methods don’t return “false” when they fail (for example read IO  
class).  
But they generate exceptions (different exceptions for each kind of  
failure).

For example, if you are handling a TCP connection and try to send a  
message,  
if it fails you’d like to know the cause:

- TCP connection closed.
- ICMP error.
- …

Maybe TCPSocket#puts method return a different exception in case of  
failure  
for the above cases (is just an example), and this info can be very  
useful  
for you.

So, sometimes is more useful to match exceptions instead of getting the  
return  
value of a method.

Regards.

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [July 1, 2008, 12:48am UTC](https://www.ruby-forum.com/t/ruby-good-practice/141846/5 "2008-07-01T00:48:25Z")

</div>

IÃ±aki Baz C. wrote:

> El Martes, 1 de Julio de 2008, Justin To escribiÃ³:
> 
> > 1. What’s the stance on global variables? When to use them, if at all?
> 
> AFAIK try not using them (except if they are really needed).

Agreed. Prefer constants, class variables, then global variables, in  
that order.

> > 1. Two arrays that have related info. (i.e. a[0] relates to b[0]) or one  
> > hash?
> 
> A Hash, of course (IMHO).

If the problem domain prefers arrays, you can iterate them using  
Array#zip:

a.zip(b).each {|ai,bi| … } # ai is a[i], bi is b[i], where i = 0 to  
max(a,b)

Always use data structures that best suit the problem domain, IMHO.
