1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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