Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "#{String}" a common idiom in Ruby

Tags:

string

ruby

A Ruby dev I know asked this; my answer is below... Are there other, better reasons?

Why do so many Ruby programmers do

"#{string}"

rather than

string

since the second form is simpler and more efficient?

like image 687
Tetsujin no Oni Avatar asked Dec 31 '25 07:12

Tetsujin no Oni


2 Answers

Is this a common idiom for Ruby developers? I don't see it that much.

like image 90
scottd Avatar answered Jan 01 '26 19:01

scottd


Smaller changes when you later need to do more than simply get the value of the string, but also prepend/append to it at the point of use seems to be the best motivation I can find for that idiom.

like image 45
Tetsujin no Oni Avatar answered Jan 01 '26 20:01

Tetsujin no Oni