[Ruby] How to use slice for beginners

Overview

The ** slice ** code came out as an answer with a Ruby drill, and I couldn't understand it easily, so I will leave it as a memorandum.

problem

There are any two strings. Ignore case and select ** True ** if one character is at the end of the other If not, create a program that outputs ** False **. (That is, it is not case sensitive).

Answer

Ruby


def end_other(a,b)
  a_down = a.downcase
  b_down = b.downcase
  a_len = a_down.length
  b_len = b_down.length
  if  a_down.slice!(-(b_len)..a_len - 1) == b_down #← I will explain this line!
    puts "True"
  else
    puts "False"
  end
end

puts "Please enter the alphabet"
code = gets.chomp

puts "Please enter the characters you specify"
find_word = gets.chomp

end_other(code, find_word)

Commentary

** slice ** returns the substring after removing the specified range from the string. Was: grin: Reference: https://docs.ruby-lang.org/ja/2.3.0/method/String/i/slice=21.html

Example


string = "this is a string"
string.slice!(2)        #=> "i"
string.slice!(3..6)     #=> " is "
string.slice!(/s.*t/)   #=> "sa st"
string.slice!("r")      #=> "r"

: warning: Count starts from ** 0 (zero) **! I stumbled here: sweat:

Therefore, the above answer can be explained in detail as follows: point_up:

Commentary


def end_other(a,b)
  a_down = a.downcase  
  #Convert all letters of variable a to lowercase!
  b_down = b.downcase
  #Convert all letters of variable b to lowercase!
  a_len = a_down.length
  #Get the number of characters in variable a!
  b_len = b_down.length
  #Get the number of characters in variable b!
  if  a_down.slice!(-(b_len)..a_len - 1) == b_down
  #-(b_len) :Returns the number of characters from the last character of variable a to the number of characters of variable b
  #a_len - 1 :Returns the last character of the variable a
  #.. :Above, represents the following
#abridgement

It looks like this, but honestly, even myself? ?? ?? So I will actually apply the characters: grinning:

Commentary commentary www


a = wOrD
b = Rd

def end_other(a,b)
  a_down = a.downcase  
  #wOrD ⇒ word
  b_down = b.downcase
  #Rd ⇒ rd
  a_len = a_down.length
  #4 characters
  b_len = b_down.length
  #2 letters
  if  a_down.slice!(-(b_len)..a_len - 1) == b_down
  #-(b_len) :Returns the last two characters of word ...>is r
  #a_len - 1 :Returns the last character of word ...>d
  #a_down.slice!(-(b_len)..a_len - 1) :Will be rd

#abridgement

It may be a little confusing. .. .. .. Sorry for my lack of writing: bow:

I hope it helps you a little: laughing:

reference

About slice About ..

Recommended Posts

[Ruby] How to use slice for beginners
How to use Ruby return
Ruby: How to use cookies
[For super beginners] How to use autofocus: true
[Ruby basics] How to use the slice method
How to use GitHub for super beginners (team development)
How to use Ruby on Rails
[Ruby] How to use any? Method
How to use Ruby inject method
[For Ruby beginners] Explain how to freely delete array elements!
How to use binding.pry for view files
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
Ruby: CSV :: How to use Table Note
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[For beginners] How to debug in Eclipse
[For beginners] How to get the Ruby delayed railway line name
How to use Map
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
Scraping for beginners (Ruby)
[Ruby] How to use gsub method and sub method
How to use an array for HashMap keys
[For beginners] How to implement the delete function
Output of how to use the slice method
[Rails] How to use Gem'rails-i18n' for Japanese support
How to use nginx-ingress-controller with Docker for Mac
[Ruby on Rails] How to use session method
How to implement Pagination in GraphQL (for ruby)
How to build a Ruby on Rails environment using Docker (for Docker beginners)
(For beginners) [Rails] Time saving tech! How to install and use slim
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Ruby] How to use standard output in conditional branching
How to implement login request processing (Rails / for beginners)
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
How to use an array for a TreeMap key
[Rails] How to use enum
Rails / Ruby: How to get HTML text for Mail
How to use java Optional
How to use JUnit (beginner)
[For beginners] How to operate Stream API after Java 8
[Rails] How to use enum