name = "James Kirk", starship = "USS Enterprise" Given the variables above, write a Ruby expression that uses string interpolation in order to produce: "The captain of the USS Enterprise is James Kirk"
Asked
Active
Viewed 140 times
-4
-
1Have you tried anything yet? – LinkBerest Apr 17 '15 at 05:12
-
What is your question? – spickermann Apr 17 '15 at 05:15
-
possible duplicate of [How to use string interpolation](http://stackoverflow.com/questions/25472826/how-to-use-string-interpolation) – LinkBerest Apr 17 '15 at 05:19
-
2No I will not take your orders. – sawa Apr 17 '15 at 05:20
-
1This sounds like a question from a learning Ruby course? – maniacalrobot Apr 17 '15 at 07:56
-
@maniacalrobot Yes, it is. and i have been doing a small mistake in answering it(use of puts statement).So,wanted to know if there is any other way of doing it. – Sandeep Chahal Apr 17 '15 at 10:28
-
@JGreenwell I've tried the code below name = "James Kirk", starship = "USS Enterprprise" puts "The captain of the #{starship} is #{name}" – Sandeep Chahal Apr 17 '15 at 10:30
1 Answers
0
So interpolation works like the following. You first need to have double quotes like using the ""'s. Then inside of those double quotes you just add the # and then the {} which contains the block of code that you want to run. So in this case you have 2 assignments:
name = "James Kirk"
starship = "USS Enterprise"
Would result in you "interpolating" the variables in the string such as this:
what_you_want = "The captain of the #{starship} is #{name}"
Hope this helps! If not, please leave a comment and I'll elaborate.

Tony DiNitto
- 1,244
- 1
- 16
- 28
-
your variable assignment is a syntax error should be: name, starship = "James Kirk", "USS Enterprise" – LinkBerest Apr 17 '15 at 14:24
-
Thanks for pointing this out JGreenwell. I didn't think he would actually assign it like that, but some people might see it and actually incorrectly try to assign variables like that. I edited my answer for clarity. – Tony DiNitto Apr 17 '15 at 21:34