I’m using string interpolation and inside that I’m using doubles quotes in split function as below:
var1 = “Ruby-Code”
result = “Hello #{var1.split(”-“)[0]}”
Here, I have used double quotes instead of single quotes in split function and the main Hello Ruby string is also declared in double quotes. So, won’t it will cause error?
Is it okay to do so? Is it the best practice won’t impact anything?
Because in other languages like python if we use double quotes inside double quoted f-string then it will cause error.
Hi @Ayusoft,
Yes, your use of string interpolation in Ruby is correct. Inside the double-quoted string, anything within #{}
is treated as Ruby code and evaluated when the string is processed. This allows for inserting dynamic content or executing logic directly within the string.
What you’ve done is standard and doesn’t cause any issues. However, while double quotes are necessary for interpolation, it’s generally best to use single quotes when you don’t need interpolation or special characters, as this can be slightly more efficient and is recommended by most style guides.
For instance, since you’re not interpolating within the split
method, you could use single quotes around the hyphen in split('-')
.