I am reviewing the drill to strengthen my logical thinking. As a beginner, I would appreciate it if you could let me know if you have any questions.
There are any two strings. Let's write a program that outputs True if either string is at the end of the other string, or False if it is not. It is not case sensitive.
If you pass'world' and'helloworld', it will be True, but if you pass'world' and'worldhello', it will be False. The latter is because the second string contains'world', but not at the end of the string.
Output example: end_other('Kilimanjaro', 'jAro') → True end_other('Everest', 'REST') → True end_other('Chomolungma', 'NgMA') → True
def end_other(a, b) a_down = a.downcase b_down = b.downcase #① if a_down.end_with?(b_down) || b_down.end_with?(a_down) #② puts "True" else puts "False" end end
(1) Since the condition is "case insensitive", the character string passed as an argument in the downcase method is converted to lowercase.
(2) Use the end_with? method to check if there is another character string after the character string.
def end_other(a, b) a_down = a.downcase b_down = b.downcase a_len = a_down.length b_len = b_down.length if b_down.slice(-(a_len)..- 1) == a_down || a_down.slice(-(b_len)..- 1) == b_down puts "True" else puts "False" end end