Rubyize this : 2nd edition

Woooah… my first post in 3 weeks. Don’t worry, it will get back to the normal soon. I have a new job and, well, you know what it is… the stress and all the new stuff to learn. Anyway, my contract will expire by the end of August so things will run smoothly again pretty soon.
Yay! Another edition of Rubyize this. For those who don’t know what it is, you just have to rewrite the code snippet into some solid and idiomatic Ruby. This is a scientific process that is also called “Rewriting a code snippet into some solid and idiomatic Ruby”.
This week we dive a bit more into the Rails world. Take for grant that the following code is located inside a view :

<%songs = Song.find(:all)
for each song in songs%>

<%=song.title[0..0].upcase + song.title[]%>

<% rating = song.rating if rating.nil? rating = 0 end if rating > 0 for i in 1..rating%> star <% end else%> This song hasn't been rated yet <%end end%>

Yikes… how ugly.
You can also specify some of your changes in plain english if you want (e.g. I would put this in a controller, I would use a partial to…, etc)
IMPORTANT NOTE : Since you cannot indent your code in the comments, you can try to put some dots or underscores instead of spaces… that way your code should remain fairly easy to read. Also, DON’T use <% and %> because it will get stripped out by wordpress! Use something else… like just the percentage sign “%”. I’m sorry about all these quirks… I absolutely have to make the comments “Ruby friendly”. For now, I strongly suggest that you copy your snippet in your clipboard BEFORE sending it. That way, if some of your code get stripped out, you won’t be that pissed off and you probably won’t try to kill me. That’s a win-win situation.

Ruby is dynamically AND strongly typed

What is a strongly typed language? Are dynamic languages like Python and Ruby automatically weakly typed? Is strong just another word for static and weak just another word for dynamic? I’m lost… help me Fleebie!
#1 Dynamic Vs Static
First step to avoid confusion : Don’t mix Dynamic/Static typing with Strong/Weak typing. Ruby, for example, is dynamically and strongly typed at the same time (more on that later).
Example of a language statically typed :

int x;
x = 3;
x = "hello"; //ERROR!

Example of a language dynamically typed :

x = 3
x = "Transformers seems to be a boring movie"
x = :why_are_we_here
x = /is this a reg ex/
x = "enough already... I think we understand"

When we talk about dynamic typing, we talk about a mechanism where the type of a variable can change and be resolved on the fly at the exact moment it gets parsed by the interpreter.
When we talk about static typing, we talk about a mechanism where the type of a variable is resolved in advance by the interpreter/compiler. With statically typed languages, you cannot say that x is a string and, a few lines after, that it is an integer. That’s all there is to say about it.
As you already know, Ruby is dynamically typed. Javascript falls in the same category as well.
#2 Strong Vs Weak
Question : Why Ruby is a strongly typed language?
Answer : Because once it knows the type of an object, it expects you to do something that makes sense with it.
In ruby, you CAN do :

x = "3"
y = x + "ho!" #result : "3ho!"

but you CANNOT do :

x = "3"
y = x + 3 #Wooo! Ruby won't like that

What do you want to do with the string “3” ? Append the Fixnum 3? What? Are you crazy? Hey look everyone, that guy want to append the string “3” to the Fixnum 3! Let’s make fun at him!
(Python and PHP are looking at each other and Python says : You knew that Ruby was still on medication?)…

Oh sh**! My geek meter just exploded…
You see, It’s not because Ruby lets you change the type of an object as many times as you want that it doesn’t care about what you do with it. If you start mixing several types together in an expression, it won’t try to understand what it means, nah… that is too complicated. Instead, ruby will throw an exception… end of story. This is why we say that Ruby is a strongly typed language.
Javascript however is dynamically but weakly typed because it lets you mix types together in your expressions without throwing an exception. The danger? If you’re not careful you will get weird results.

x = call_function_that_returns_the_number_3_in_the_string_form;
y = x + 1 //The programmer expect that "y" will equal 4 (Tee hee hee!)

after that instruction, y will contain “31”. Hmm, that could be a bug hard to spot. Javascript is being more hippie than Ruby on this level. It’s like : relax man… do watcha want with your types, I don’t care. I’ll mix them all and we will have a good laugh watching the results. It will be like “art”! For this reason, Javascript is a weakly typed language.
So, to conclude :
A dynamically typed language is a language where the type of a variable can be altered at any time. (It is a string, now it is a Fixnum, now it is a Time object, etc.)
A statically typed language is the opposite. (Decide what x is once for all and don’t change your mind!)
A strongly typed language is a language that is being strict about what you can do with your typed variables. (Don’t mix them… or I will throw you an error in the face!)
A weakly typed language is the opposite. (Do what you want with your different types. Mix them all! We’ll see what happens!)

How to return multiple values from a method

The following is easy stuff but we tend to forget about it… probably because most of us are not used to this kind of behavior from a programming language.
Ruby gives the illusion that you can return more than one element from a method.
This is how you do it :

def a_method_to_insult_innocent_people
  error = compute_error
  if error == :stupid
    return false, "You made a stupid error"
  elsif error == :ridiculous
    return false, "You made a ridiculous error"
  elsif error == :worst_of_all_time
    return false, "You made the most idiot error in history. Way to go..."
    return true, "You made no error, you are still an idiot"
success, msg = a_method_to_insult_innocent_people

It’s important to note that even though it doesn’t look like it, only ONE thing is returned from this method. This thing is, you bet, an array.
In fact,

return false, "you made a stupid error"

is equivalent to :

return [false, "you made a stupid error"]

Ruby arranges things both on the sending and receiving end to make it look like you are returning more than one element.