Codementor Events

Ruby vs Java

Published Feb 17, 2018Last updated Feb 22, 2018

As someone who primarily used Java as his first language, finding out I had to use Ruby for my first full-time job was well…a little nerve-wracking. Five months after, here is what I learned.

Printing To Console

Java

System.out.println("Hello World!");

Ruby

puts “Hello World!”

Not much else to say here.

Other Cool Things

I dont have a problem with Java's syntax. Its not bad, but its not that great either.

public static void main(String[] args) { 
  int result = 0;
    for(int i=0;i<5;i++){
    	result+=i
    }
  if(result > 5){
    	return result;
    }
  if(result < 50){
    	return 50;
    }
}

Nothing too crazy here. Let’s check the ruby version of this.

def main 5.times.do |i| @result+=i end return result if result > 5 return 50 if result < 50end

This is just easier to read and understand in my opinion.

Like Java, Ruby is also an OOP (Object Oriented Programming) language. Therefore, efficent class design is crucial to the implementation of robust, maintainable applications. One of the fundamental principles of OOP is Encapsulation. This is implemented in Java through the use of getter and setter methods.

public class Student {
private String name; public String getName(){ return name; }
public void setName(String name){ this.name = name; }
}

Student student = new Student('Jacob');System.out.println(student.getName());student.setName('Jake');

Encapsulation is important, so creating these methods are necessary, but cumbersome. Ruby provides us with a much more compact solution

class Student attr_accessor :name
def initialize(name) @name = name end end
student = Student.new('Jacob')puts student.namestudent.name = 'Jake'

The ‘attr_accessor’ essentially serves as the getter and setter method for the instance variable name. This is much more convienient than taking the time to write seperate methods for each instance variable in the class.

Method arguments are another area where ruby is able to really shine. One neat feature is hash arguments. Lets say we have a method that creates a Student object, and this method’s arguments will be used to update the fields of the object.

Java

public Student newStudent(String name, int id, String email, String cellNumber){ return new Student(name, id, email, cellNumber);}

Ruby

def create_student(args)Student.new(args[:name],args[:id],args[:email], args[:cell_number]end

...

create_student(name:'Jacob',id:1,email:'jacob@email',cell_number:'000-000-0000')

What’s nice here is that we are able to keep the method compact and more readable. Explicitly having to write out each attribute as a parameter in Java makes the code much more cluttered.

There are a lot more examples I could write about that highlights ruby’s ease of use, but I’ll stop here.

So here is my takeaway: Ruby syntax is very clean and concise. Because of this, writing beautiful code is an essential part of being a great ruby programmer.

So for now, I bid farwell to you Java.

Discover and read more posts from Jacob Abraham
get started
post commentsBe the first to share your opinion
Show more replies