gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 06:55:51 26 4
gpt4 key购买 nike

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

类似于:

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/

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