Finding the difference between strings in Ruby

前端 未结 5 982
长发绾君心
长发绾君心 2021-02-07 19:00

I need to take two strings, compare them, and print the difference between them.

So say I have:

teamOne = \"Billy, Frankie, Stevie, John\"
teamTwo = \"Bi         


        
5条回答
  •  野的像风
    2021-02-07 19:25

    You need to sort first to ensure you are not subtracting a bigger string from a smaller one:

    def compare(*params)
       params.sort! {|x,y| y <=> x}
       diff = params[0].split(', ') - params[1].split(', ')
       if diff === []
          true
       else
          diff
       end 
    end
    
    puts compare(a, b)
    

提交回复
热议问题