<%if @item.rigged %>Yes<%else%>No<%end%>
我在想这样的东西?
if @item.rigged ? "Yes" : "No"
但这并不奏效。Ruby有||=,但我甚至不知道如何使用那个东西。
<%if @item.rigged %>Yes<%else%>No<%end%>
我在想这样的东西?
if @item.rigged ? "Yes" : "No"
但这并不奏效。Ruby有||=,但我甚至不知道如何使用那个东西。
当前回答
shell和C的单行结构都可以工作(ruby 1.9.3p429):
# Shell format
irb(main):022:0> true && "Yes" || "No"
=> "Yes"
irb(main):023:0> false && "Yes" || "No"
=> "No"
# C format
irb(main):024:0> true ? "Yes" : "No"
=> "Yes"
irb(main):025:0> false ? "Yes" : "No"
=> "No"
其他回答
If else条件可以用三元运算符覆盖
@item.rigged? ? 'Yes' : 'No'
从if @item中删除if。操纵?“是”:“不是”
三元运算符有窗体条件吗?If_true: if_false
if apple_stock > 1
:eat_apple
else
:buy_apple
end
上面的语句可以在ruby中写成三元语句的形式:
apple_stock > 1 ? :eat_apple : :buy_apple
别忘了它可以从ruby标签中生成内容:
v
<%= @item.rigged? ? "Yes" : "No" %>
标记(v)表示使用等号在html中生成文本。
据我所知
3一行程序
如果<条件>,A = 10
例子:
a = 10 if true # a = 10
b = 10 if false # b = nil
A = 10,除非<条件>
例子:
a = 10 unless false # a = 10
b = 10 unless true # b = nil
A = <条件> ?<a>: <b>
例子:
a = true ? 10 : 100 # a = 10
a = false ? 10 : 100 # a = 100
我希望这能有所帮助。