Why this form of using case does not work?

0

I have the following code:

def label_file_type(type)
    label = 
    case type.to_s
    when ['.jpg', '.jpeg', '.png'].include?(type) then
        content_tag(:span, 'Imagem', class: ['label', 'picture'])
    when ['.mp3'].include?(type) then
        content_tag(:span, 'Áudio', class: 'label audio')
    when ['.txt', '.pdf', '.doc', '.docx', '.odt'].include?(type) then
        content_tag(:span, 'Documento', class: 'label document')
    else
        content_tag(:span, 'Indefinido', class: 'label')
    end
end

This is a helper. For some reason, the only output I get on the screen is that of the else. It is as if the instruction is ignoring all comparisons and jumping straight to the last. Can anyone explain me why this does not work that way?

    
asked by anonymous 20.04.2015 / 17:14

1 answer

1

The syntax should be:

def label_file_type(type)
    label = 
    case type.to_s
    when '.jpg', '.jpeg', '.png' 
        content_tag(:span, 'Imagem', class: ['label', 'picture'])
    when '.mp3' 
        content_tag(:span, 'Áudio', class: 'label audio')
    when '.txt', '.pdf', '.doc', '.docx', '.odt' 
        content_tag(:span, 'Documento', class: 'label document')
    else
        content_tag(:span, 'Indefinido', class: 'label')
    end
end
    
22.04.2015 / 19:22