Ruby convenience methods for fledgling engineers

Introduction

** As a fledgling engineer, I write down a method that was very useful while solving algorithm problems with AtCoder and paiza. ** **

Differences between puts, p and print

I tried to explain it in my own words, but there were a lot of articles on the web, so I decided to stop.

[Introduction to Ruby] Differences between print, puts, and p https://yukimasablog.com/ruby-print-puts-p [Ruby] Differences between p, puts, and print methods https://qiita.com/rkkmshde/items/daf75aca9675f5a01d17

If you can master it when debugging, you will notice errors and their causes quite efficiently when writing complex code.

Ternary operator

if 〜 else 〜 end Can be written more efficiently.

number = 24
if number.even? 
    n = "Even"
else
    n = "Odd"
end
puts n  
# =>Even

This process is ...

number = 24
n = number.even? ? "Even" : "Odd"
puts n  
# =>Even

It will be like this.

** Condition? (Processing when condition is true): (Processing when condition is false) **

It is a structure.

map

#Input value
# 12 30 40
input = gets.split.map(&:to_i)
#Output value
# [12, 30, 40]
#It becomes an array like this.

It can be used when you want to divide the numerical value or character string of the input value into individual elements and make an array.

** "&:" ** I will omit the explanation of the method this time. For more information, https://qiita.com/soma_sekimoto/items/a828b8f88b78aac2e7df Refer to this article.

any?, all? And one?

These methods are very useful when you want to use iterative processing to see if the conditions are met **.

When I didn't know this, I was trying to substitute each for everything, so the code was ridiculous.

array = [30, 14, 23, 15]
array.any? {|num| num % 7 == 0} #Elements divisible by 7(14)There is
# => true  

** any? **: Returns true if at least one of each element meets the conditions.

** all? **: Returns true if all elements meet the conditions.

** one? **: Returns true if only one of each element meets the conditions.

reduce

(1..10).reduce(5) {|a, n| a + n}
# => 5 + 1 = 6,This 6 becomes a of the next process.
#In other words, the second process is 6+It becomes 2.
#Processing result=> 60

In the reduce method, set two arguments (a and n) in the block process. a is the initial value (here 5) and n is each element (here, an integer from 1 to 10).

between?

23.between?(0, 30)
# => true

'G'.between?('A', 'F')
# => false

A method to determine if the specified values (here 23 and'G') are within the range (0-30 and A-F)

index


array = ["ruby", "java", "php"]
p array.index("java")
# => 1
# "java"Index number is returned.

transpose ** A method that considers an array as a matrix ** and swaps rows and columns when the array contains other arrays as elements **

array = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
p array.transpose
# => [[1,5,9], [2,6,10], [3,7,11], [4,8,12]]

He was very active in algorithmic problems that used the idea of rows and columns.

chars

greeting = "hello"
p greeting.chars
# => ["h", "e", "l", "l", "o"]

As described above, ** the character string is divided into characters and returned as an array **.

You can also write like this.

num = "19800".chars.map(&:to_i)
p num
# => [1, 9, 8, 0, 0]

It can be used when you want to process digit-by-digit numbers. zip This zip method is useful when you want to process multiple arrays at the same time.

number = [1, 2, 3, 4, 5]
alphabet = ["a", "b", "c", "d", "e"]
p number.zip(alphabet)
number.zip(alphabet) do |num, alpha|
    p num
    p alpha
end

# => [[1, "a"], [2, "b"], [3, "c"], [4, "d"], [5, "e"]]

# => 1
# => "a"
# => 2
# => "b"
...
# => 5
# => "e"

As mentioned above, it can be returned as an array, or it can be processed by using the each method for multiple arrays.

select, reject ** select **: {} Extract only the elements whose expression is true in the block. ** reject **: {} Extract only the elements whose expression is false in the block.

p [1, 2, 3, 4, 5, 6].select { |n| n % 2 == 0 }
p [1, 2, 3, 4, 5, 6].reject { |n| n % 2 == 0 }
# => [2, 4, 6]
# => [1, 3, 5]

in conclusion

Compared to other languages, ruby has more methods, so I realized that I could write intuitive code, and I became more fond of ruby.

I would like everyone to refer to it when writing algorithms using ruby.

Recommended Posts

Ruby convenience methods for fledgling engineers
Getting Started with Ruby for Java Engineers
Ruby Learning # 15 Methods
About Ruby methods
abbreviation for ruby method
Ruby Learning # 31 Object Methods
About Ruby instance methods
Plan for 2019 fledgling engineers to run without looking back
Ruby variables and methods
[Ruby] methods, instance methods, etc ...
Ruby Learning # 23 For Loops
Scraping for beginners (Ruby)
Basic methods of Ruby hashes
Basic methods of Ruby arrays
Three Bit Manipulation Methods (Ruby)
[Ruby] What is `!!` used for?
What are Ruby class methods?
[Ruby] Singular methods and singular classes
[Ruby] Class methods, instance methods, etc.
Ruby variables and functions (methods)
Ruby methods and classes (basic)
[Ruby] Singular methods and singular classes