Ruby always returns the last value used in the method; it's pretty
redundant to use return in most cases.  In your example, I see no
reason to use return.  I see that
Digest::SHA1.hexdigest(string_to_hash) was the last value used in the
method, so therefore I know that's what is coming back to the code
that called it.

It's a Ruby idiom, but it's not as confusing as some. :)

--Jeremy

On 10/19/07, Tito Ciuro <tciuro / mac.com> wrote:
> Hello,
>
> I have a question about skipping the 'return' keyword in a Ruby method.
> For example:
>
> def self.encrypted_password(password, salt)
>   string_to_hash = password + salt
>   Digest::SHA1.hexdigest(string_to_hash)
> end
>
> Being used to work in other languages, this is weird to me. Without
> looking at the documentation, I have no way of knowing that 'hexdigest'
> returns a string. Wouldn't the following be easier to understand?:
>
> def self.encrypted_password(password, salt)
>   string_to_hash = password + salt
>   return Digest::SHA1.hexdigest(string_to_hash)
> end
>
> Why so many Ruby snippets skip the 'return' keyword?
>
> Thanks,
>
> -- Tito
> --
> Posted via http://www.ruby-forum.com/.
>
>


-- 
http://www.jeremymcanally.com/

My books:
Ruby in Practice
http://www.manning.com/mcanally/

My free Ruby e-book
http://www.humblelittlerubybook.com/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/