[Ruby] How to write blocks

Overview

Since I learned about blocks to deepen my understanding of Ruby, I will output it.

What is a block?

--do ~ end is called a block in Ruby.

For example, such a problem because it is easier to understand if there is an example. (There are other ways to solve it, but this time the times sentence) ** A block is an argument of the times method. It is repeated by writing in do ~ end so that iterative processing is performed according to the contents of the block. ** **

[Problem] Add the numbers from 1 to 10 in order, and output the result of adding all at the end to the terminal.

sum = 0

10.times do |i|
  sum += i + 1
end

puts sum

#Terminal output result
# 55

How to write a block

In the above problem, I used do ~ end. There are other ways to write blocks. For example, it looks like this.

sum = 0
10.times { |i| sum += i + 1 }

puts sum

#Terminal output result
# 55

By enclosing do ~ end with{}, Ruby recognizes it as a block.

Each person has their own way of writing, but keep in mind that there is such a way of writing. It seems that {} is used when it fits in one line, and do ~ end is used for long code including line breaks.

yield

A program that calls and outputs normal methods

The normal method is called and puts is output.


##Normal calling
def normal
  puts 'nomal method call'
end

normal

#Terminal output result
#nomal method call

Execute block using yield

The block (do ~ end) passed to the nomal method is called.

##Call the block passed when calling the nomal method with yield
def normal
  puts 'nomal method call'
  yield
end

normal do
  puts 'Call with yield'
end

#Terminal output result
#nomal method call
#Call with yield

application

Add an argument to yield

By putting an argument in yield when calling the block, any thing (argument value) is passed to the block.


def normal
  puts 'nomal method call'
  yield('Give an argument')
end

#The argument is passed to the block by giving an argument to yield
normal do |text|
  puts text
end

#Terminal output result
#nomal method call
#Give an argument

Explicitly described

point --nomal (& block argument) --Block argument .call (argument you want to pass to the block)


def normal(&block) #In the argument block&Put on
  puts "nomal method call"
  block.call("Give an argument") #Block with call method(block)Run
end

normal do |text|
  puts text
end

Summary

--There are "do ~ end" and "{}" in the block. ――You can write clearly by using "{}". --Use "{}" if it fits on one line, and "do ~ end" if you need a line break. You can execute the block passed to the method by using --yield. --~~ If you want to execute the block explicitly, add & before the argument. ~~ --~~ The block received as an argument can be called with the "call method". ~~ --In order to receive the block as a Proc object, add & before the argument. --Use the call method to receive and execute the block.

References

-Ruby 2.7.0 Reference Manual -[Block? yield? ] A solid understanding of Ruby's Proc objects

Recommended Posts

[Ruby] How to write blocks
Studying Java # 6 (How to write blocks)
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to write code that thinks object-oriented Ruby
How to write good code
How to use Ruby return
How to write java comments
[Refactoring] How to write routing
[Ruby] How to comment out
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
How to write Rails routing
[Ruby on Rails] How to write enum in Japanese
How to iterate infinitely in Ruby
How to install ruby through rbenv
How to use Ruby on Rails
Baseball ball count (how to write)
How to install Bootstrap in Ruby
[Ruby] How to use any? Method
How to write a ternary operator
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to use Ruby inject method
How to execute Ruby irb (interactive ruby)
Comparison of how to write Callback function (Java, JavaScript, Ruby)
Introduction to Ruby 2
[Ruby on Rails] How to use CarrierWave
[Basic] How to write a Dockerfile Self-learning ②
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
Offline real-time how to write F03 ruby and C implementation example
How to deploy
Ruby length, size, count How to use
[Java] How to output and write files!
How to write ruby if in one line Summary by beginner
[Ruby] How to use slice for beginners
[Ruby on Rails] How to use redirect_to
[Easy] How to upgrade Ruby and bundler
[Ruby on Rails] How to use kaminari
How to write Spring AOP pointcut specifier
Ruby: CSV :: How to use Table Note
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
How to find the cause of the Ruby error
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Ruby] How to convert CSV file to Yaml (Yml)
[JavaFX] How to write Eclipse permissions in build.gradle
How to write offline 15th reference question answer
[Rails] How to write when making a subquery