SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ruby - conditional against array

    so let's say i have an array...
    ["pdf", "doc", "jpg", "tif", "gif"]

    and i have a VALUE
    "pdf"

    how would i do something like
    Code:
    if VALUE equals any element in ARRAY
      do something
    end
    is this even possible this easily OR do i have to loop through the array testing the value each time? like so

    Code:
    ARRAY.each do |array|
      if array.value == VALUE
        do something
      end 
    end
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The Array method you want is include? (question mark is part of the method name).
    Code:
    file_extensions = ["pdf", "doc", "jpg", "tif", "gif"]
    if file_extensions.include?("pdf")  
      #do stuff
    end
    BTW, Ruby documentation can be found here:
    http://www.ruby-doc.org/core/

    Very useful for finding methods of this type.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •