Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the characters inside two strings are the same in Ruby

Tags:

ruby

I have two strings, a and b, in Ruby.

a="scar"
b="cars"

What is the easiest way in Ruby to find whether a and b contain the same characters?

UPDATE
I am building an Anagram game ,so scar is an anagram of cars.So i want a way to compare a and b and come to conclusion that its an anagram
So c="carcass" should not be a match

like image 394
Stormvirux Avatar asked May 31 '26 04:05

Stormvirux


1 Answers

You could do like this:

a = 'scar'
b = 'cars'
a.chars.sort == b.chars.sort
# => true

a = 'cars'
b = 'carcass'
a.chars.sort == b.chars.sort
# => false
like image 162
toro2k Avatar answered Jun 01 '26 18:06

toro2k