Compare against array

I have a database field called file_type. I want to compare it against
an array of types to either display it as an image or just create a link
to it. I am sure there is an easy way to do this but I can’t think of
it.

I don’t want to do this:

<% if resource.file_type == “.png” OR resource.file_type == “.jpg” …
%>

I would like to do:

<% if resource.file_type == [".png", “.jpg”, “.gif”, …] %>

What do you think?

Thanks.

Seth

Use …
<% if [".png", “.jpg”, “.gif”, …].include?(resource.file_type) %>

… it looks quite nice.

David M.

Seth B. wrote:

I have a database field called file_type. I want to compare it against
an array of types to either display it as an image or just create a link
to it. I am sure there is an easy way to do this but I can’t think of
it.

I don’t want to do this:

<% if resource.file_type == “.png” OR resource.file_type == “.jpg” …
%>

I would like to do:

<% if resource.file_type == [".png", “.jpg”, “.gif”, …] %>

What do you think?

Thanks.

Seth

That’s it!

Looks good. Thanks.

Unicode hacks - problem wrote:

Use …
<% if [".png", “.jpg”, “.gif”, …].include?(resource.file_type) %>

… it looks quite nice.

David M.

On Jun 22, 2006, at 20:03, Seth B. wrote:

%>

I would like to do:

<% if resource.file_type == [".png", “.jpg”, “.gif”, …] %>

I’d create a helper

def image_type?(file_type)
[".png", “.jpg”, “.gif”, …].include?(file_type)
end

And perhaps another one to generate the very link.

– fxn

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs