Orsay Orsay - 16 days ago 5
Ruby Question

How to include a 'next if' condition in an inline loop

I would like to include

next if
in this loop:

= select_tag :type, options_for_select(Products.statuses.keys.map{ |product_type| [I18n.t("product.#{product_type}"), product_type] }, params[:type])


So I would like something as:

Products.statuses.keys.map{ |product_type| next if product_type == "clothes", [I18n.t("product.#{product_type}"), product_type] }

Answer

Having a list you can always select or reject elements based on condition:

Products.statuses
        .keys
        .reject { |product_type| product_type == "clothes" } # <= won't be in list
        .map    { |product_type| [I18n.t("product.#{product_type}"), product_type] }
Comments