Ruby: Merging variables in to a string

前端 未结 7 967
[愿得一人]
[愿得一人] 2020-12-02 10:23

I\'m looking for a better way to merge variables into a string, in Ruby.

For example if the string is something like:

\"The animal action<

7条回答
  •  再見小時候
    2020-12-02 10:40

    This is called string interpolation, and you do it like this:

    "The #{animal} #{action} the #{second_animal}"
    

    Important: it will only work when string is inside double quotes (" ").

    Example of code that will not work as you expect:

    'The #{animal} #{action} the #{second_animal}'
    

提交回复
热议问题