Ruby - Help with select method

Discussion in 'Programming' started by Rofellos, Jul 2, 2013.

  1. Rofellos

    Rofellos New Member

    Joined:
    Dec 1, 2010
    Messages:
    7
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    **** some girls
    Location:
    Dantes Inferno
    Hi,

    I have a problem in Ruby that I can't solve, I already tried to google it, but nothing.

    I hava an array. I want to use the select method to select tthe elements that contains the string "YES" or the string "NO".

    It doesn't work:

    a.select { |x| x.include? "YES" || x.include? "NO" }

    Can anyone help me?

    Thanks!
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    This works :)

    Code:
    #!/usr/bin/ruby
    
    a = ["Pradeep","Yes","YES","NO"]
    
    print a.select { |x| x.include?("YES") || x.include?("NO") }
    
     
    shabbir likes this.
  3. Rofellos

    Rofellos New Member

    Joined:
    Dec 1, 2010
    Messages:
    7
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    **** some girls
    Location:
    Dantes Inferno
    Thank you, it works!

    It works only with the '(' and ')', and without the white space after 'include?'.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice