Ruby 迭代一个变量,除非它是 nil
Ruby iterate over a variable unless it is nil
我想在 .html.erb 中使用一种简洁的方法来仅在变量不为 nil 时循环遍历该变量。
我希望执行以下命令,但如果@family 为 nil 则不会。
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
我尽量避免做这样的事情
<% if @family %>
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
<% end %>
尤其是尽量避免需要
<% if @family && @family.children %>
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
<% end %>
有更好的方法吗?
这个怎么样:
<% @family && @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
此解决方案可能会产生误导,但 Ruby 的语法允许您这样做:
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end unless @family.blank? %>
# ^^^^^^^^^^^^^^^^^^^^^
我只将此解决方案用于简单的语句,例如测试对象的存在(如您的情况)。 我不建议将此解决方案用于更复杂的逻辑,因为第三方不会知道条件位于块的末尾。
另一个:
<% (@family.try(:children) || []).each.with_index(1) do |family_member, index| %>
# mu-is-too-short's (brilliant) suggestion:
<% @family.try(:children).to_a.each.with_index(1) do |family_member, index| %>
如果 @family
是 nil
,try(:children)
不会引发错误,但会 return nil
,然后 nil || []
returns "you can loop on it" 的空数组(实际上循环零次)。
可以用if @family.present吗?或者相反,除非@family.blank?
也许你可以在你的控制器中安装这个?
@family ||= []
您可以使用 Null Object,例如:
class NullFamily
def children
[]
end
end
在你的控制器中:
@family = some_finder || NullFamily.new
或者您可以传递一个单独的变量 @children
:
@family = some_finder
@children = @family.try(:children).to_a
并将循环更改为:
<% @children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
我想在 .html.erb 中使用一种简洁的方法来仅在变量不为 nil 时循环遍历该变量。
我希望执行以下命令,但如果@family 为 nil 则不会。
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
我尽量避免做这样的事情
<% if @family %>
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
<% end %>
尤其是尽量避免需要
<% if @family && @family.children %>
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
<% end %>
有更好的方法吗?
这个怎么样:
<% @family && @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>
此解决方案可能会产生误导,但 Ruby 的语法允许您这样做:
<% @family.children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end unless @family.blank? %>
# ^^^^^^^^^^^^^^^^^^^^^
我只将此解决方案用于简单的语句,例如测试对象的存在(如您的情况)。 我不建议将此解决方案用于更复杂的逻辑,因为第三方不会知道条件位于块的末尾。
另一个:
<% (@family.try(:children) || []).each.with_index(1) do |family_member, index| %>
# mu-is-too-short's (brilliant) suggestion:
<% @family.try(:children).to_a.each.with_index(1) do |family_member, index| %>
如果 @family
是 nil
,try(:children)
不会引发错误,但会 return nil
,然后 nil || []
returns "you can loop on it" 的空数组(实际上循环零次)。
可以用if @family.present吗?或者相反,除非@family.blank?
也许你可以在你的控制器中安装这个?
@family ||= []
您可以使用 Null Object,例如:
class NullFamily
def children
[]
end
end
在你的控制器中:
@family = some_finder || NullFamily.new
或者您可以传递一个单独的变量 @children
:
@family = some_finder
@children = @family.try(:children).to_a
并将循环更改为:
<% @children.each.with_index(1) do |family_member, index| %>
// HTML HERE
<% end %>