[Ruby] Method memorandum

Introduction

Write down the method as a memorandum

gets method

Gets the value entered by the user on the keyboard as a string There is always a line break at the end

Standard input
abcd

input = gets
p input

Output result
"abcd\n"  

chomp method

Remove the trailing newline character and return a new string

Standard input
abcd

input = gets.chomp
p input

Output result
"abcd" 

split method

Split a string into a array of strings

Standard input
hello new world

array = gets.split(" ")
p array

Output result
["hello", "new", "world"]

readlines method

Gets the multi-line value entered by the user on the keyboard as an array of strings`

Standard input
hello
new
world

array = readlines.chomp
p array

Output result
["hello", "new", "world"]

map method

Iterate as many times as there are elements in the array

array = ["1", "2", "3"]
new_array = array.map{ |v| v.to_i }
p new_array

Output result
[1, 2, 3]

join method

Convert each element of the array to a string and combine the arguments as delimiters

array = ["hello", "new", "world"]
p array.join(" ")

Output result
"hello new world"

push method

Add an argument as an element to the end of the array

array = ["a", "b", "c"]
p array.push("d")

Output result
["a", "b", "c", "d"]

Recommended Posts

[Ruby] Method memorandum
[Ruby] slice method
[Ruby] end_with? method
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
Ruby Learning # 30 Initialize Method
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
Ruby Thread # [] = method notes
Recently learned method, memorandum ①
definition of ruby method
ruby memorandum (basic edition)
[Ruby] Method definition summary
Method
Integer check method with ruby
Ruby algorithm (inject, method definition)
memorandum
[Ruby] Notes on gets method
[Ruby] Method that returns truth
memorandum
Ruby on Rails Basic Memorandum
[ruby] Method call with argument
Ruby design pattern template method pattern memo
[Ruby] Method to count specific characters
[Ruby] present/blank method and postfix if.
[Ruby] Extracting elements with slice method
[Ruby basics] split method and to_s method
[Ruby] How to use any? Method
String output method memo in Ruby
Memorandum (Ruby: Basic grammar: Iterative processing)
Ruby memorandum (acquisition of key value)
[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
[Ruby] Obtaining even values ​​using the even? Method
to_i method
Ruby basics
Ruby learning 5
Ruby basics
java (method)
Ruby Review 2
getRequestDispatcher () method
Ruby addition
merge method
MyBatis memorandum
Refactoring Ruby
Ruby on Rails installation method [Mac edition]
Java memorandum
[Ruby] From the basics to the inject method
Ruby learning 3
Implemented "Floyd Cycle Detection Method" in Ruby
Rails memorandum
Map method
Error memorandum
include method
Abstract method