Cut out a Ruby string

Introduction

It will be a memo for learning

Since two integers separated by spaces and a character string are input, output a substring of the range of two integers.

Input example 1
2 6
this is a pen

Output example 1
his i
Input example 2
2 6
Welcome to the paiza! I`m studying ruby!

Output example 2
elcom

Source code

nums = gets.chomp.split(" ")
str = gets.chomp.split("")

for i in (nums[0].to_i - 1)..(nums[1].to_i - 1)
     print str[i]
end

Commentary

nums = gets.chomp.split(" ")
str = gets.chomp.split("")

Nums is the output range, and str is the character string decomposed and assigned to the input numbers and character strings.

gets method: Receives input as a "character string" line by line. chomp method: Removes line breaks in character strings. split method: Splits a character string into an array.

for i in (nums[0].to_i - 1)..(nums[1].to_i - 1)

Convert the range from nums [0] to nums [1] to an integer with the for statement and assign it to the i variable.

print str[i]

Output the range of i variables in the string of str

Finally

I would appreciate it if you could point out any mistakes.

Recommended Posts

Cut out a Ruby string
[Ruby] Cut out a string using the slice method
[Java] How to cut out a character string character by character
[Java] How to use substring to cut out a character string
Extract a part of a string with Ruby
[Java] How to use substring to cut out a part of a character string
Various Ruby string operations
Count the number of occurrences of a string in Ruby
Ruby Regular Expression Extracts from a specific string to a string
Ruby Learning # 2 Drawing a Shape
[Ruby] Extracting a two-dimensional array
What is a Ruby module?
Convert to Ruby Leet string
How to change a string in an array to a number in Ruby
Creating a calendar using Ruby
Ruby Learning # 11 Building a Calculator
[Ruby] How to comment out
Multiplication in a Ruby array
The nth and n + 1st characters of a Ruby string
Create a fortune using Ruby
Ruby Learning # 32 Building a Quiz
Ruby Learning # 8 Working With String
Ruby ① Build a Windows environment
Explanation about Ruby String object
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Sorting hashes in a Ruby array
Ruby Learning # 19 Building a Better Calculator
Ruby: I made a FizzBuzz program!
[Ruby] It's a convenient guy ~ before_action ~
Ruby Learning # 22 Building a Guessing Game
String output method memo in Ruby
Implement a gRPC client in Ruby
What is a Ruby 2D array?
Make a typing game with ruby