Methods that I found useful in Ruby

Write down the methods that you find useful.

Count the number of occurrences of a character string

One letter

str = "aaabbbcccabc"  str.count("a") => 4

Multiple characters

str = "aaabbbcccabc"  str.scan("ab") => [ab, ab]  str.scan("ab").length => 2

Get absolute value

num = 5  num.abs => 5 num = (-5)  num.abs => 5

Replace part of the string

str = "aabbcc"  str.gsub("aa", "") => str = bbcc

Replace aa with an empty string

Delete part of the string

Delete from the end

str = "aabbcc"  str.chomp("cc") => aabb

Delete anywhere

str = "aabbcc"  str.delete("bb") => aacc

Uppercase ⇄ Lowercase

str = "abc"  str.upcase => ABC  str.downcase => abc

Array

Generate

(1..5).to_a => [1, 2, 3, 4, 5]

add to

end

array = ["a", "b", "c"]  array.push("e"); array = ["a", "b", "c", "e"]

lead

array = ["a", "b", "c"]  array.unshif("e"); array = ["e", "a", "b", "c"]

Delete

array = ["a", "b", "c", "e"]  array.delete("e") array = ["a", "b", "c"]  array[0,2] = [] array = ["c"]

Remove two from 0 subscript

Replace

array = ["a", "b", "c"]  array.map!{|x| x=="a" ? "z" : x} => ["z", "b", "c"]

Joining two arrays

arrayX = [1, 2, 3]  arrayY = ["a", "b", "c"]  arrayX.concat(arrayY) arrayX = [1, 2, 3, "a", "b", "c"]

Remove duplicates

array = [1, 1, 2, 2, 3, 3]  array.uniq => [1, 2, 3]

Invert

array = ["a", "b", "c"]  array.reverse! => ["c", "b", "a"]

Split the string character by character

str = "abc"  str.chars => ["a", "b", "c"] num = 12345  num.chars => [1, 2, 3, 4, 5]

Creating a combined string

array = ["a", "b", "c"]  array.join => "abc"

Recommended Posts

Methods that I found useful in Ruby
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
Ruby methods often used in Rails
I tried a calendar problem in Ruby
[Ruby basics] Methods that frequently use blocks
I found MyBatis useful, so I wrote it.
I started Ruby
I want to use arrow notation in Ruby
Class in Ruby
I made a Ruby extension library in C
Heavy in Ruby! ??
Ruby Learning # 15 Methods
[Ruby] Methods that can be used with strings
About Ruby methods
I want to get the value in Ruby
[Ruby / namespace] Deepen ":: ← this" that you often see in Ruby
Autoboxing that I learned as NullPointerException in Short comparison
[Ruby] Misunderstanding that I was using the module [Beginner]
In Ruby, methods with? Do not always return true/false.
About eval in Ruby
Ruby Learning # 31 Object Methods
About Ruby instance methods
Ruby variables and methods
Output triangle in Ruby
[Ruby] methods, instance methods, etc ...
Variable type in ruby
Fast popcount in Ruby
Definitely useful! Debug code for development in Ruby on Rails
I made an interpreter (compiler?) With about 80 lines in Ruby.
Determine that the value is a multiple of 〇 in Ruby
I want to perform high-speed prime factorization in Ruby (ABC177E)
I searched for a web framework with Gem in Ruby
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I want to create a Parquet file even in Ruby
I wrote a C parser (like) using PEG in Ruby
I touched the devise controller that I felt in the black box