DEV Community

Felipe Brigatto
Felipe Brigatto

Posted on • Originally published at Medium on

Java x Ruby: Differences for beginners

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 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]
Enter fullscreen mode Exit fullscreen mode

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

vector << 1 # [1]
vector << [2,3] # [1,[2,3]]
Enter fullscreen mode Exit fullscreen mode

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

ArrayList<Integer> vector = new ArrayLista<Integer>;
vector.add(1);
Enter fullscreen mode Exit fullscreen mode

With that we got freedom of when and what insert new element into 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 rules:

  • If carrier is blank, add shipping_method if is filled, otherwise add blank String
  • If carrier and method are filled, add them concatenated.
  • Otherwise, add blank String.

Not thinking too much, in Ruby we 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
Enter fullscreen mode Exit fullscreen mode

And in 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
Enter fullscreen mode Exit fullscreen mode

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, 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
Enter fullscreen mode Exit fullscreen mode

With this, we guarantee we have an information as a result to our little method. If both values are blank, it satisfies the basic 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 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(" - ")
Enter fullscreen mode Exit fullscreen mode

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

Originally published at medium.com on May 2, 2017 in Portuguese.

Top comments (0)