2012-11-25 200 views
7

字符串我想將一個字符串中的變量中的條件,同時也對可變如何包括紅寶石

類似的情況:

x = "best" 

"This is the #{if !y.nil? y else x} question" 

字符串我可以在外面做y||x。我在字符串內做什麼?

回答

14
"This is the #{y.nil? ? x : y} question" 

"This is the #{y ? y : x} question" 

"This is the #{y || x} question" 

可以使用y||x內插值就像外面

4

您可以絕對做一個字符串中相同

y = nil 
x = "best" 

s = "This is the #{y || x} question" 
s # => "This is the best question" 
2

使用三元運算:

"This is the #{!y.nil? ? y : x} question"