[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase

Overview

I used the ** downcase ** method in my Rails tutorial. I haven't had a chance to use it before, so I would like to deepen my understanding while solving the problem this time. I would also like to take a brief look at related methods.

table of contents

--Related methods

--Methods used in the following problems - start_with? - end_with?

--Practice --Problem --Answer --Another solution

--Summary

--References

Related methods

Method How to use
downcase Convert uppercase to lowercase
upcase Convert lowercase letters to uppercase
swapcase Convert uppercase to lowercase and lowercase to uppercase
capitalize Convert first lowercase letters to uppercase
Destructive method(!) At the end of the above method**( ! )When you giveobject**Make changes to

Method used in the following problem

start_with?

A method to check if it starts with the specified string

end_with?

A method to check if it ends with the specified string

Practice

problem

There are any two strings. Write a program that compares strings and checks if the other string is included at the end of the string.

answer

I'm using downcase because I don't want to distinguish between lowercase and uppercase. It works fine with all uppercase letters using the upcase method.

def end_other(a, b)

  #Convert to lowercase and store in variable
  a_down = a.downcase 
  b_down = b.downcase

  #The character string of b is included in the character string of a?Or is the string a in the string b?
  if a_down.end_with?(b_down) || b_down.end_with?(a_down)
    p 'True'
  else
    p 'False'
  end
end

#Method call
end_other('Apple', 'Ple')
end_other('orange', 'Range')
end_other('banana', 'Nana')

Another solution

By using slice, the part specified by the argument is cut out. For slice, please refer to here.

def end_other(a, b)

  #Convert to lowercase and store in variable
  a_down = a.downcase
  b_down = b.downcase

  #Get the length of a string
  a_len = a_down.length
  b_len = b_down.length

  #Cut the character of the character string of a from the character string of b and equal to the character string of a.?Or, cut the character of the character string of b from the character string of a and equal to the character string of a.?
  if b_down.slice(-(a_len)..-1) == a_down || a_down.slice(-(b_len)..-1) == b_down
    p 'True'
  else
    p 'False'
  end
end

#Method call
end_other('Apple', 'Ple')
end_other('orange', 'Range')
end_other('banana', 'Nana')

Summary

--You can easily convert from lowercase to uppercase, from uppercase to lowercase, etc. by using the methods provided by Ruby.

References

-Ruby 3.0.0 Reference Manual (downcase)

-Ruby 3.0.0 Reference Manual (upcase)

-Ruby 3.0.0 Reference Manual (capitalize)

-Ruby 3.0.0 Reference Manual (swapcase)

-Ruby 3.0.0 Reference Manual (start_with?)

-Ruby 3.0.0 Reference Manual (end_with?)

-Summary of methods and usages that can be used with Ruby strings

-What is the slice method? Let's solve the example and understand the difference from slice!

-[Ruby] Extracting elements with slice method

Recommended Posts

[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
Ruby How to convert between uppercase and lowercase
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
How to convert LocalDate and Timestamp
[Rails] How to convert from erb to haml
[Easy] How to upgrade Ruby and bundler
Convert JSON to TSV and TSV to JSON with Ruby
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
How to get and add data from Firebase Firestore in Ruby
[Java] How to convert from String to Path type and get the path
[Ruby] How to convert CSV file to Yaml (Yml)
[Ruby] How to use gsub method and sub method
From Java to Ruby !!
[ruby] How to receive values from standard input?
Convert Java enum enums and JSON to and from Jackson
How to convert A to a and a to A using AND and OR in Java
How to handle TSV files and CSV files in Ruby
[Ruby] How to get the tens place and the ones place
(Ruby on Rails6) How to create models and tables
How to use Ruby return
Convert to Ruby Leet string
[Ruby] How to comment out
Convert from ○ months to ○ years ○ months
Ruby: How to use cookies
[Ruby] How to write blocks
How to convert Java radix
How to migrate from JUnit4 to JUnit5
[Java] How to convert a character string from String type to byte type
How to write and notes when migrating from VB to JAVA
How to deal with different versions of rbenv and Ruby
[Ruby] How to batch convert strings in an array to numbers
[IntelliJ IDEA] How to display code completion candidates without distinguishing between uppercase and lowercase letters
[Ruby On Rails] How to search and save the data of the parent table from the child table
How to use StringBurrer and Arrays.toString.
How to push from Tarminal to GitHub
Convert ruby object to JSON format
How to use EventBus3 and ThreadMode
How to iterate infinitely in Ruby
How to install ruby through rbenv
How to use Ruby on Rails
How to convert erb file to haml
How to call classes and methods
How to use equality and equality (how to use equals)
How to install Bootstrap in Ruby
[Ruby] How to use any? Method
How to connect Heroku and Sequel
Introduction to Ruby (from other languages)
How to use Ruby inject method
How to execute Ruby irb (interactive ruby)
How to change from HTML to Haml
Offline real-time how to write F03 ruby and C implementation example
How to convert param to hash with Rails controller (updated from time to time)
[Swift5] How to communicate from ViewController to Model and pass a value
[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
How to get date data in Ruby
How to use OrientJS and OrientDB together
[Ruby on Rails] How to use CarrierWave
[Ruby] From the basics to the inject method
Convert numbers to Roman numerals in Ruby
Convert from java UTC time to JST time
[IOS] How to get data from DynamoDB