Re: Question on the "common way" to code

Arnaud J.
Sun, 22 Jul 2007 10:27:44 -0000

nemesis256 wrote:

> 3. dynamic links
> link_to(composer.first_name + " " + composer.last_name
> and
> link_to("#{composer.first_name} #{composer.last_name}"

To my knowledge, there is a difference between those two expressions :
the second one requires more processing than the first one, because of
the double quotes.

#{} evalutes what''s inside of the brackets and the result is
transformed
into a string. Because #{var} does not return "the content of var",
but
"the result of the expression var transformed into a string".
So, if composer.first_name returns a string already, you don''t have to
transform it again in a string.

Another example. If you define number = 5, you can use
"#{number} blabla"
as well as
number.to_s + ''blabla''
There you can see where lies the difference. For numbers, you have to
use .to_s if you use concatenate (+), a transformation that isn''t
needed
for your composer.first_name (apparently).

Besides, double quotes means that the whole expression will be parsed to
look not only for #{}, but also for other escape caracters like "\n".
This means more processing.

So, the first solution is less redundant (with maybe some performance
effects?), and the second is more readable. Make your choice.

Please, do correct me if I''m wrong, since I started RoR less than a
week
ago :)

--
Posted via http://www.ruby-forum.com/.