In Ruby, can you perform string interpolation on data read from a file?

前端 未结 8 1847
抹茶落季
抹茶落季 2020-12-10 10:37

In Ruby you can reference variables inside strings and they are interpolated at runtime.

For example if you declare a variable foo equals \"Ted\

8条回答
  •  天涯浪人
    2020-12-10 11:05

    The 2 most obvious answers have already been given, but if they don't to it for some reason, there's the format operator:

    >> x = 1
    => 1
    >> File.read('temp') % ["#{x}", 'saddle']
    => "The number of horses is 1, where each horse has a saddle\n"
    

    where instead of the #{} magic you have the older (but time-tested) %s magic ...

提交回复
热议问题