Archive for the ‘ruby’ Category.

Why Ruby and Rails are so delightful

Since the first time I typed the first lines of code, Ruby and Rails made me feel good and comfortable. I fell comfortable about the language itself, the syntax, and the amazing simplicity, that I want to show here ;)

Starting with Ruby, I can say that it is a very nice, clear and concise language. Of course I had some hard time, because it’s a dynamic language, that has a different philosophy rather than Java and C#, that by the way are strongly typed languages. They It made me remember my old friend clipper :)

I’d like to show below some nice thing I like in this language, such as a kind of “for”, or manipulating arrays :

10.times do print “Hello, world!” end

resultado : Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!
Hello, world!Hello, world!Hello, world!Hello, world!

x = []
x << “Word”
x << “Play”
x << “Fun”

As I told you before, Ruby is a dynamic language, so, there is no need to assign a type, but if we had to do the same thing in Java we would need it :

String[] x = new String[3];
x[0] = “Word”;
x[1] = “Play”;
x[2] = “Fun”;

Hmmm ok, somebody can ask me “but, what is the difference ?? they look similar !”. To answer this question I can say that they really seems to share the same idea, but it’s wrong. As I told before Ruby is a dynamic language, so, I can rest without having to worry about pre-defined types. Another good point about Ruby is that it provides me some “utility” methods that other languages don’t :

x = ["Word", "Play", "Fun"]
puts x.join(‘, ‘)
resultado : Word, Play, Fun

x = [1, 2, 3, 4, 5]
y = [1, 2, 3]
z = x – y
puts z.inspect
result : [4, 5]

The last thing I want to show today about Ruby is the Interpolation capability, as a piece of code can tell 100 times more than 200 pages, there we go :

x = 5
y = 7
puts “#{x} + #{y} = #{x + y}”
resultado : 5 + 7 = 12

This code blows up my mind ;)

Now, introducing Rails framework, it’s a web framework that was developed using Ruby language, and Rails implements the MVC pattern, and a persistence layer via Active Record. I can tell that SIMPLICITY was what made me love it. I was remembering what I need to develop a web application using Java : JSP, TagLibs, Struts ou JSF, Spring, Hibernate, configure a dozen of XML files and/or annotations, and a lot more, for sure, it’s too much !

It’s hard to deny that the below code is much more simple to write and mantain than all steps above :

class MyClass < ActiveRecord::Base

validate_presence_of :date, :value

validates_numericality_of :value

belongs_to :user

protected

def validate

errors.add(:value, “Should be positive”) if value.nil? || value <= 0

end

end

To summarize, the point here is neither try to prove that Ruby and Rails are the best things in the universe, nor, I’m saying that they gonna be the next plasma cannon to defeat any space invader :)

Instead, I’m trying to point out everything that makes this language and framework so great. Also, Ruby has limitations as any other language, and Rails doesn’t solve some problems, as pointed out by Martin Fowler on his Enterprise Rails post. Anyway, both Ruby and Rails deserve good attention, because they are easy, flexible and very comfortable. I strongly recommend you to try it if you haven’t so far !