[Ruby] Ruby API problem

Erase the nth character for any character and
create a method to output the erased character

** How to call ** missing_char(string, num)

** Output example: ** missing_char('kitten', 1) → 'itten' missing_char('kitten', 2) → 'ktten' missing_char('kitten', 4) → 'kiten'

  1. ** slice method ** By using the slice method, you can extract the specified element from the array or character string. When specifying the element of the character string, use a number, and the first character string is counted from 0.
#Create a string
string = "abcdefg"

#Get the element specified by the argument from the character string and assign it to the variable
str = string.slice(2)

#Output the character string assigned to str
puts str
#=> "c"

#The string remains as it is
puts string
#=> "abcdefg"
  1. ** slice! Method ** A method with an exclamation mark (!) At the end is called a destructive method. This is a method that modifies the original array or string. You can delete the specified element by using the slice! method.
string = "abcdefg"
str = string.slice!(2)
puts str
#=> "c"

# "c"Has been removed
puts string
#=> "abdefg"

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

Use the ** slice! Method ** to strip the value obtained from the string itself to erase the nth character for any character. However, since the ** slice method ** cannot change the shape of the character string itself, it is changed to the ** slice! method ** with an exclamation (!).

This slice! method does the processing inside the missing_char method and The argument str of the missing_char method is the input character string, and n is a mechanism to enter a number that indicates which character string to delete. And the slice! method takes the number obtained by subtracting 1 from n as an argument. Why subtract 1? Even when specifying the order of character strings, the first character string is counted from 0, just like an array.

[Order of character strings](Human    123    s t r    012 [Order of subscripts](Machine

The order of letters seen by humans is 1 to The order of letters seen from the machine is 0 to Since it is a translation from human to machine, I could understand it with the feeling of "n -1". It's deep.

Recommended Posts

ruby API problem
[Ruby] Ruby API problem
ruby API problem
Ruby problem ⑦
ruby search problem
[Ruby] FizzBuzz problem
Use Face API from Ruby
[Ruby] problem with if statement
Ruby deposit system, algorithm problem
Calendar creation problem (fun Ruby practice problem)
[Ruby] Search problem using index method
This problem is soberly difficult ... (Ruby)
Ruby learning 4
Make Ruby Kurosawa with Ruby (Ruby + LINE Messaging API)
I tried a calendar problem in Ruby
[Ruby] Array
Rails API
Call API [Call]
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
FizzBuzz problem
Ruby setting 2
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
[Problem] Weather on consecutive holidays (Ruby edition)
Ruby Review 1
[Ruby] Module
Procedure for operating google spreadsheet with API (ruby)
[Competition Pro] Solve the knapsack problem with Ruby
Try to get redmine API key with ruby