Sep 24, 2009

9 Things I Like About Ruby, So Far

#1. Parallel assignment
short and sweet

a, b, c = 1, 2, 3
a = b = c = 5


#2. Easy OOP
From free codes, modules, classes
Everything is an object

Standard methods are great, you can even add on methods too:
class String
  def my_method
    puts "Awesome!"
  end
end

Then you can have "text".my_method

#3. Readable
The language is compact and readable

(1..5).each do |n|
  puts n
end
# hash is a comment

You can use { } or do end as blocks too



One line does it all:

a ||= 10

Instead of:

if !defined? a then
  a = 10
end

You got any more neat tricks to share?


#4. Ruby have Symbols
Starts with colon :tryme

Makes things much readable, it is not a string, but acts like a string.
Weird. But simple and elegant.

Imagine that it is a variable with string value of its own name.


#5. Hashes are Easy
These are just cool pair values (I created my own pairs class back in VB6)

Define it this way:

p = {} #initialize as array first
p['lousy'] = 1
p['moderate'] = 2
p['good'] = 3
p['great'] = 4


or this way, all in one line

p = {'lousy' => 1, 'moderate' => 2, 'good' => 3, 'great' => 4}


even tastier when you use :symbols with hashes

p = { :lousy => 1, :moderate => 2, :good => 3, :great => 4 }


#6. Flexible Variables
No more type mismatch!

In VB,
Dim f as Integer
f = "Try Me" 'Type mismatch error happens


You can assign different data types to any variables any time.
f = 1.5
f = "Try Me" #no sweat


#7. Ruby have great web frameworks
Rails, Merb, Sinatra, Ramaze and more


#8. Working with Strings, creating harmony to your code
#{var} is an easy way for value replacements within strings

We can also have methods like #{var.method} assuming it returns a string

Multi line strings are easy to work with
letter = <<WHATEVER
  Dear #{name},
  This is the content of the email...
  ...
  #{profile.signature}
WHATEVER

#9. It's Open Source
Need to say more?

#10. Not in the list
Oh! And by the way, you can test some codes out with irb (Mac / Linux) or fxri (in Windows)

Learn Ruby in 20 minutes -> this is where I started.

And to avoid confusion, try to learn up and familiarize with Ruby codes before looking at Rails. I got confused and I thought this tip may help you. :)

No comments:

Post a Comment