Orsay Orsay - 10 months ago 45
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 Source

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

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