[Ruby] slice 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

For any string, use the slice method to erase the nth character, Let's create a method that outputs the character string with the nth character erased.

How to call

missing_char(string, num)

Output example

missing_char ('Genghis Khan', 1) →'Nghis Khan' missing_char ('Genghis Khan', 2) →'Jigisukan' missing_char ('Genghis Khan', 4) →'Genghis Khan'

Tips

slice You can extract the specified element from an array or character string. When specifying the element of the character string, use a number, and the first character string is counted from 0.

slice! ~~ A method with an exclamation mark (!) At the end is called a destructive method. A method that changes the original array or string. ~~ !! It is not the only way to distinguish between destructive and non-destructive. Many are paired, but that's not a Ruby rule. This ↓ was a great learning experience. The presence or absence of! In a Ruby method name does not mean destructive / non-destructive

The slice! method can remove the specified element.

Model answer


def missing_char(str, n)
  str.slice!(n - 1)
  puts str 
end


#call
missing_char('Genghis Khan', 1) 

#result
 'Ngiscan'

Commentary

This time, I want to output the character string with the nth character erased, so I will use the slice! Method with exclamation (!).

The flow is explained in the model answer as follows. ① At the caller, give an instruction to erase the "1" first character of "Genghis Khan". (2) "Genghis Khan" is passed to str and "1" is passed to the formal argument of n. ③ Erase the "1st" character of "Genghis Khan" in str with slice!

One thing to note here is the (n -1) next to slice !. As mentioned in the hint, the first string is counted from 0. Therefore, if you do not do -1, the first "n" of "Genghis Khan" will be erased and it will become "" Jigisukan "".

So

str.slice!(n - 1)

By doing, you can erase the characters you really want to erase from str.

Recommended Posts

[Ruby] slice method
Ruby to_s method
[Ruby] end_with? method
[Ruby] Method memorandum
[Ruby] Extracting elements with slice method
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
Extract characters from Ruby strings slice 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
[Ruby basics] How to use the slice method
[Ruby] Cut out a string using the slice method
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] Method to count specific characters
[Ruby] present/blank method and postfix if.
[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
[Ruby] Array
Java method
[Ruby] Obtaining even values ​​using the even? Method
Ruby basics
java (method)
Ruby Review 2
Ruby addition
merge method
Refactoring Ruby
Ruby on Rails installation method [Mac edition]
[Ruby] From the basics to the inject method
Ruby learning 3
Map method
include method
Abstract method
Ruby print puts p printf output method
initialize method
List method
puts method
Ruby setting 2
[Ruby] How to use slice for beginners
ruby Exercise Memo II (variable and method)
Java method
Class method
Ruby problem ⑦
save! method
getParameter method