Saturday, March 21, 2015

Why Ruby is Amazing

First of all, I love Ruby. Let's get that out first. If it wasn't clear by the title of this post, it is now.
Second, my open classes example will not be used in this as it is primarily opinion-based. Now, on to the amazingness.

Easy Looping Through Arrays

Quick comparison of Java code to Ruby code:
// Java
int[] array = {1, 5, 3, 7};
for (int number : array) {
System.out.println(number);
}
vs # Ruby
array = [1, 5, 3, 7]
array.each {|num| puts num}
Well, in Ruby we just saw a few things. We saw the puts function, the lack of parentheses, and blocks. A block is just that, a block of code.

Gems

At this point, I think there's a gem for everything. There's even a gem for NBT, although its docs are awful. Need a IRC framework? We have that. Hey, we have a ton of them. Need a web framework? Ruby on Rails, Sinatra, and there's some others still. Take your pick.

It Follows the Perl Philosophy

For reference, this is having many ways to do the same thing. Not much else to say here. Moving on!

The Syntax

Although this is primarily opinion-based, which do you think looks better, this basic Java class or this basic Ruby class? //Java
class Greeter {
String name;
public Greeter(String firstname, String lastname) {
name = firstname + " " + lastname;
}
public void sayHi() {
System.out.println("Hi "+name+"!");
}
}
# Ruby
class Greeter
def initialize(first_name, last_name)
@name = "#{first_name} #{last_name}"
end
def say_hi
puts "Hi #{@name}!"
end
end

No comments:

Post a Comment