在 Ruby create 中放入 if 语句

问题描述 投票:0回答:1
Example.create(
  attribute1 = "asdf",
  attribute2 = "asdf2",
  attribute3 = "and 20 more attributes"
)

但是我如何方便地制作

variable2 = "qwer"
if
random_thing == "zxcv"
,而不必在 if/else 中使用两个(几乎)相同的创建方法?

ruby if-statement class-method
1个回答
0
投票

也许通过使用三元运算符:

Example.create(
  attribute1 = "asdf",
  attribute2 = random_thing == "zxcv" ? "qwer" : "asdf2",
  attribute3 = "and 20 more attributes"
)
© www.soinside.com 2019 - 2024. All rights reserved.