let's go to the bar 我有很多包含特殊字符的字符串,偶-6ren">
gpt4 book ai didi

ruby - 您可以对带有插值的字符串使用 ruby​​ 百分比表示法吗?

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:22 24 4
gpt4 key购买 nike

我在 ruby​​ 中使用模板字符串。示例:

foo = "bar"
"let's go to the #{foo}" #=> let's go to the bar

我有很多包含特殊字符的字符串,偶然发现了 %q 分隔符技巧,它非常有用。

%q["I don't respect them", he said] #=> "\"I don't respect them\", he said"

不幸的是,它似乎不适用于插值。

catchphrase = 'wubba lubba dub dub'
%[My new catchphrase is "#{catchphrase}"] #=> "My new catchphrase is \"\#{catchphrase}\""

关于如何让插值和 % 符号很好地发挥作用有什么想法吗?

最佳答案

尝试使用 %Q 而不是 %q。例如:

catchphrase = 'wubba lubba dub dub'
%Q[My new catchphrase is "#{catchphrase}"] #=> "My new catchphrase is \"wubba lubba dub dub\""

关于ruby - 您可以对带有插值的字符串使用 ruby​​ 百分比表示法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30993047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com