Archive for the 'ruby' Category

My “Wow!” moment learning Ruby

Friday, January 12th, 2007

I´m a absolutely beginner in Ruby, but recently, after I changed to my new web host, I was tempted to make a try.

My background is mainly in Java language, with some SmallTalk just because since I left my graduation, I never saw a so beatifull syntax. And until now, I write sometimes somethings in SmallTalk, when I need to think about the problem in hand without worry about the environment or language little tricks and details. After all, I think compiled and typed languages are doomed to become history.

My new web host gave me a lot of space and bandwidth, but only 2 practical choices of programming languages for dynamic web sites: PHP or Ruby. For personal reasons I hate PHP (please, I don´t want to start a flame war about what is the best language, this is just my personal opinion). My first job was about programming in PHP, and I don´t have good memories…

I´m studying right now, and for the people which come here looking for Java tips or tricks, I want to say I had a ‘Wow!’ moment right now. Look for this little piece of code:

1.class Product < ActiveRecord::Base
2.
3.	validates_presence_of :title, :description, :image_url
4.	validates_numericality_of :price, :only_integer => true
5.
6.	protected
7.	def validate
8.		errors.add(:price, “should be positive”) if price.nil? || price <= 0
9.	end
10.end

Don´t worry about what this code do. But the syntax is very cool, isn´t it? For me, some constructions doesn´t seem so strange, because they look like Smalltalk, but for a JavaMan… You can see the ‘if’ block? Or the method call in a null reference?

I see you in my next “Wow!” moment…