Java x Ruby: Differences for beginners
Tags: softwaredevelopment, rails, introduction, beginners
Published: 2017-10-30 01:12:59

---
title: "Java x Ruby: Differences for beginners"
published: true
tags: software-development,ruby-on-rails,introduction,beginners
canonical_url: 
---

![](https://cdn-images-1.medium.com/proxy/1*zVdJoDXQMbh91HasFKCuTg.jpeg)

One of my first programming experience with Ruby on Rails was pleased. Obviously each language has it’s own way to write vicious code, so I’ll share my first steps with&nbsp;Ruby.

The basic difference is you don’t initialize objects, we can use own structs to initialize. In Java, there is something not like, but is a beginning:

Example:

```
// Java
int[] vector_ = new int[5];
int[] vector_pre_defined = {1,2,3,4,5};

// Ruby
vector = []
vector_pre_defined = [1,2,3,4,5]
```

Structurally seem the same, but in Ruby we can increment easily:

```
vector << 1 # [1]
vector << [2,3] # [1,[2,3]]
```

In Java, we need to create an ArrayList to do almost the same thing than&nbsp;Ruby:

```
ArrayList<Integer> vector = new ArrayLista<Integer>;
vector.add(1);
```

With that we got freedom of when and what insert new element into&nbsp;Array.

Now come a practical example: we have a JSON about an order from e-commerce, in it there is shipping info not yet filled. We need to fill it based on this&nbsp;rules:

- If carrier is blank, add shipping\_method if is filled, otherwise add blank&nbsp;String
- If carrier and method are filled, add them concatenated.
- Otherwise, add blank&nbsp;String.

Not thinking too much, in Ruby we&nbsp;have:

```
if not order.shipping_carrier
 values << order.shipping_method || ""
elsif order.shipping_carrier && order.shipping_method
 values << order.shipping_carrier + " - " + order.shipping_method
else
 values << ""
end
```

And in&nbsp;Java:

```
if !order.getShippingCarrier() {
 if order.getShippingMethod() value = order.shipping_method;
 else value = “”;
} else if order.shipping_carrier && order.shipping_method {
 value = order.shipping_carrier + " - " + order.shipping_method;
} else
 value = "";
end
```

Using a sequential logic, those codes are hard to read and didn’t use advantages of both languages!

To start refactoring in Ruby, there is a compact function of Arrays, that returns without null values,&nbsp;so:

```
[order.shipping_method, order.shipping_carrier].compact
# [“Method”] # if shipping_carrier blank
# [“Carrier”] # if shipping_method blank
# [“Method”,”Carrier”] # if both are filled
# [] # if both are blank
```

With this, we guarantee we have an information as a result to our little method. If both values are blank, it satisfies the basic&nbsp;rule.

Com isso garantimos ter sempre a informaÃ§Ã£o Ãºtil, mesmo se estiver nula teremos um array vazio. que podemos converter para uma String vazia. EntÃ£o com nosso novo array, podemos juntar seus valores usando o mÃ©todo&nbsp;join.

There is one more function to use: `join()` that let use a String to concatenate String.

Finally, with one line we satisfy all conditions:

```
values << [order.shipping_method, order.shipping_carrier].compact.join(" - ")
```

That was my every first code in Ruby a year ago, I started in Java, and that was a paradigms break, because there are many manners to write a descent code and only practice (and cultural) will turn bad code into good&nbsp;ones.

_Originally published at_ [_medium.com_](https://medium.com/skyhub-labs/java-x-ruby-diferen%C3%A7as-ao-programar-8fb675b2cde) _on May 2, 2017 in Portuguese._