[Ruby] end_with? method

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.

problem

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

Model answer


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

Commentary

(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.



The answer for the drill was as follows, but I used end_with? Because it was troublesome. If you have any suggestions or reasons why the following methods are better, please let us know.
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

Recommended Posts

[Ruby] end_with? method
Ruby to_s method
[Ruby] slice method
[Ruby] Method memorandum
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
definition of ruby method
[Ruby] Method definition summary
Method
Integer check method with ruby
Ruby algorithm (inject, method definition)
[Ruby] Notes on gets method
[Ruby] Method that returns truth
[ruby] Method call with argument
Ruby design pattern template method pattern memo
[Ruby] Method to count specific characters
[Ruby] present/blank method and postfix if.
[Ruby] Extracting elements with slice method
[Ruby basics] split method and to_s method
[Ruby] How to use any? Method
String output method memo in Ruby
[Ruby] Search problem using index method
[Ruby on Rails] Convenient helper method
[Ruby] undefined method `dark?'occurs in rqr_code
How to use Ruby inject method
Ruby learning 4
Java method
to_i method
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
getRequestDispatcher () method
Ruby addition
merge method
Refactoring Ruby
Ruby on Rails installation method [Mac edition]
[Ruby] From the basics to the inject method
Ruby learning 3
Implemented "Floyd Cycle Detection Method" in Ruby
Map method
include method
Abstract method
Ruby print puts p printf output method
initialize method
List method
puts method
Ruby setting 2
ruby Exercise Memo II (variable and method)
Java method
Class method
Ruby problem ⑦
save! method
getParameter method
Ruby learning 2