ruby - 如何在 ruby​​ 中的字符串中包含条件

标签 ruby string

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

类似于:

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/

相关文章:

javascript 数字、字符串、数学和正确的方法

python : replace multiple occurrence of characater by one but single occurrence by none

ruby-on-rails - 如何允许用户从 Rails 中的多选框中选择零个选项?

python 文件串联和组合文件

ruby-on-rails - 使用 foreach 函数从 csv 文件中读取数据

ruby-on-rails - 如何正确格式化 json 以使用 RestClient 发送

python - 在不丢失精度的情况下将 `float` 类型转换为 `str`

Python 字符串的所有可能组合

ruby - 消除 <head> 问题,我需要指导/帮助

ruby-on-rails - 具有命名参数的 RSpec 测试方法