如何在ruby中的字符串中包含一个条件

Nic*_*nto 7 ruby string

我想在一个字符串中放置一个变量,但是在变量上也有一个条件

就像是:

x = "best"

"This is the #{if !y.nil? y else x} question"
Run Code Online (Sandbox Code Playgroud)

在我能做的字符串之外y||x.我在字符串里面做什么?

小智 17

"This is the #{y.nil? ? x : y} question"
Run Code Online (Sandbox Code Playgroud)

要么

"This is the #{y ? y : x} question"
Run Code Online (Sandbox Code Playgroud)

要么

"This is the #{y || x} question"
Run Code Online (Sandbox Code Playgroud)

你可以y||x像在外面一样使用内插