Finally, create a method for whether there is any character

【Overview】

1. Conclusion </ b>

2. How to code </ b>

3. Development environment </ b>

  1. Conclusion

Use 3 types </ b> of downcase method, length method and slice method </ b>!

  1. How to code

def str_discrimination(str_a, str_b)
  a_down_str = str_a.downcase #---❶
  b_down_str = str_b.downcase
  a_len_str = a_down_str.length #---❷
  b_len_str = b_down_str.length
  if b_down_str.slice(-(a_len_str)..- 1) == a_down_str || a_down_str.slice(-(b_len_str)..- 1) == b_len_str #---❸
    puts "True"
  else
    puts "False"
  end
end

❶: This is because it is not case sensitive. All two types of strings (str_a, str_b) have been made lowercase with the downcase method </ b>.

variable(String).downcase

❷: The number of characters is returned by the length method </ b> of the variable converted to lowercase in ❶. This is coded for use in ❸.

❸: Here, it is determined whether there is any character at the end. The OR condition is set so that the character string to be searched for or the character to be searched for can come to either str_a or str_b. Also, the slice method </ b> is used, and the variable assigned by the length method </ b> used in ❷ is inserted to search for any character from the end. (-(a_len_str) ..-1) indicates the number of characters from (-1) to-(a_len_str) from the end.

Referenced URL: First Ruby! Summary of how to convert a character string to uppercase ⇔ lowercase Summary of differences between length, size, and count methods [Ruby]

  1. Development environment

Ruby 2.6.5 Rails 6.0.3.3 Visual Studio Code