我想在字符串中放置一个变量,但也有一个变量条件
类似于:
x = "best"
"This is the #{if !y.nil? y else x} question"
在字符串之外我可以做y||x
。我在字符串中做什么?
最佳答案
"This is the #{y.nil? ? x : y} question"
或
"This is the #{y ? y : x} question"
或
"This is the #{y || x} question"
你可以在插值内部像在外部一样使用y||x
关于ruby - 如何在 ruby 中的字符串中包含条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13554780/