SlideShare a Scribd company logo
1 of 28
Download to read offline
Rubyist生活に欠かせ
ないブロック構文の話
@ran_tan
Twitter: @ran_tan
Facebook: kohei.taniguchi
HAW International.inc
谷口 耕平
学びと成長サポート事業部
@飯塚
問題 10回 Ruby と出力して下さい。
問題 10回 Ruby と出力して下さい。
for i in 1..10!
puts 'Ruby'!
end
普通に書いてみる
問題 10回 Ruby と出力して下さい。
for i in 1..10!
puts 'Ruby'!
end
普通に書いてみる
もう少しRubyっぽく書いてみる
10.times do!
puts 'Ruby'!
end
問題 10回 Ruby と出力して下さい。
for i in 1..10!
puts 'Ruby'!
end
普通に書いてみる
もう少しRubyっぽく書いてみる
10.times do!
puts 'Ruby'!
end
forは繰り返し処理のための構文
問題 10回 Ruby と出力して下さい。
for i in 1..10!
puts 'Ruby'!
end
普通に書いてみる
もう少しRubyっぽく書いてみる
10.times do!
puts 'Ruby'!
end
forは繰り返し処理のための構文
timesはIntegerクラスのメソッド
問題 10回 Ruby と出力して下さい。
10.times do!
puts 'Ruby'!
end
timesがメソッドならその後ろのdo … endはなにか?
問題 10回 Ruby と出力して下さい。
10.times do!
puts 'Ruby'!
end
timesがメソッドならその後ろのdo … endはなにか?
→ブロック
ブロック構文
ざっくり言うと、
・ブロックとは処理の塊
・メソッドに渡すことが出来る
!
・高階関数を簡単に書ける画期的な構文!
ブロックを受け取るメソッド
array = [24, 21, 4, 41, 39]!
!
# 配列の値が奇数か偶数か判定!
array.each do |i|!
if i % 2 == 1!
puts "#{i} is odd"!
else!
puts "#{i} is even"!
end!
end
each
ブロックを受け取るメソッド
array = [24, 21, 4, 41, 39]!
!
# 配列の値が奇数か偶数か判定!
array.each do |i|!
if i % 2 == 1!
puts "#{i} is odd"!
else!
puts "#{i} is even"!
end!
end
each
ブロック引数
ブロックを受け取るメソッド
collect, map
array = [24, 21, 4, 41, 39]!
!
# 数字の代わりに奇数ならtrue、偶数ならfalseが入った配列を作れ!
array.collect do |i|!
i % 2 == 1!
end # => [false, true, false, true, true]!
!
!
!
!
!
!
!
!
!
!
!
!
ブロックを受け取るメソッド
collect, map
array = [24, 21, 4, 41, 39]!
!
# 数字の代わりに奇数ならtrue、偶数ならfalseが入った配列を作れ!
array.collect do |i|!
i % 2 == 1!
end # => [false, true, false, true, true]!
!
# もう少しわかりやすくしてみる!
array.collect do |i|!
i.odd?!
end # => [false, true, false, true, true]!
!
!
!
!
!
!
ブロックを受け取るメソッド
collect, map
array = [24, 21, 4, 41, 39]!
!
# 数字の代わりに奇数ならtrue、偶数ならfalseが入った配列を作れ!
array.collect do |i|!
i % 2 == 1!
end # => [false, true, false, true, true]!
!
# もう少しわかりやすくしてみる!
array.collect do |i|!
i.odd?!
end # => [false, true, false, true, true]!
!
# 1行で書いてみる!
array.collect { |i| i.odd? }!
!
!
! do … endの代わりに{ }でもいい
ブロックを受け取るメソッド
collect, map
array = [24, 21, 4, 41, 39]!
!
# 数字の代わりに奇数ならtrue、偶数ならfalseが入った配列を作れ!
array.collect do |i|!
i % 2 == 1!
end # => [false, true, false, true, true]!
!
# もう少しわかりやすくしてみる!
array.collect do |i|!
i.odd?!
end # => [false, true, false, true, true]!
!
# 1行で書いてみる!
array.collect { |i| i.odd? }!
!
# まだ長い?!
array.collect(&:odd?)!
ブロックを受け取るメソッド
collect, map
array = [24, 21, 4, 41, 39]!
!
# 数字の代わりに奇数ならtrue、偶数ならfalseが入った配列を作れ!
array.collect do |i|!
i % 2 == 1!
end # => [false, true, false, true, true]!
!
# もう少しわかりやすくしてみる!
array.collect do |i|!
i.odd?!
end # => [false, true, false, true, true]!
!
# 1行で書いてみる!
array.collect { |i| i.odd? }!
!
# まだ長い?!
array.collect(&:odd?)!
メソッドに& + シンボルを渡すと、
ブロック引数に対して
シンボルで渡した名前のメソッドが呼ばれる。
ブロックを受け取るメソッド
inject, reduce
array = [24, 21, 4, 41, 39]!
# 合計を計算してみよう!
!
array.inject(0) { |result, i| result + i } # => 129
ブロックを受け取るメソッド
inject, reduce
array = [24, 21, 4, 41, 39]!
# 合計を計算してみよう!
!
array.inject(0) { |result, i| result + i } # => 129
24
21
4
41
39
0i result
ブロックを受け取るメソッド
inject, reduce
array = [24, 21, 4, 41, 39]!
# 合計を計算してみよう!
!
array.inject(0) { |result, i| result + i } # => 129
24
21
4
41
39
24i result
ブロックを受け取るメソッド
inject, reduce
array = [24, 21, 4, 41, 39]!
# 合計を計算してみよう!
!
array.inject(0) { |result, i| result + i } # => 129
24
21
4
41
39
45i result
ブロックを受け取るメソッド
inject, reduce
array = [24, 21, 4, 41, 39]!
# 合計を計算してみよう!
!
array.inject(0) { |result, i| result + i } # => 129
24
21
4
41
39
49
i result
ブロックを受け取るメソッド
inject, reduce
array = [24, 21, 4, 41, 39]!
# 合計を計算してみよう!
!
array.inject(0) { |result, i| result + i } # => 129
24
21
4
41
39 90i result
ブロックを受け取るメソッド
# 偶数だけを取り出し、1の位でソートしたい!
[4, 21, 16, 42, 31].select(&:even?).sort{ |i, j| i % 10 <=> j % 10} !
# => [42, 4, 16]
# 偶数だけ取り出したい!
array.select { |i| i.even? } # => => [24, 4]!
!
# こちらもブロックを省略できます!
array.select(&:even?) # => => [24, 4]
array = [24, 21, 4, 41, 39]
# sortメソッド!
array.sort # => [4, 21, 24, 39, 41]!
!
# 1の位でソートしたい!
array.sort{ |i, j| i % 10 <=> j % 10} # => [41, 21, 24, 4, 39]
select
sort
+
=
ブロックを受け取るメソッド
# ブロックを受け取るメソッドはコレクション操作だけじゃないです。!
# ファイル操作の例!
!
# ブロックを使わないパターン!
file = open('example.rb')!
puts file.gets!
file.close!
!
# ブロックを使うパターン!
open('example.rb') do |file|!
puts file.gets!
end
ブロックを受け取るメソッドを定義する
# 出力をデコレートしてみる!
def decorate_output!
puts '-*-*-*-*-*-*-'!
yield!
puts '-*-*-*-*-*-*-'!
end!
!
decorate_output do !
puts 'Hello, Block!'!
end!
!
# => -*-*-*-*-*-*-!
# Hello, Block!!
# -*-*-*-*-*-*-
yieldを使います。
受け取ったブロックを実行した
い箇所にyieldと書きます。
# こんな動作をするselectメソッドを定義してみます。!
select([24, 21, 4, 41, 39]) {|i| i > 10} !
# => [24, 21, 41, 39]!
!
# yieldに引数を渡すことで、ブロックに引数を渡せます。!
def select(array)!
result = []!
array.each do |i|!
result << i if yield(i)!
end!
result!
end
ブロックを受け取るメソッドを定義する
# ブロックを引数にして受け取ることも出来ます。!
def select(array, &block)!
result = []!
array.each do |i|!
result << i if block.call(i)!
end!
result!
end!
!
# 引数として受け取ったブロックは他のブロック!
# を受け取るメソッドに渡すことが出来ます。!
def select(array, &block)!
array.select(&block)!
end!
!
ブロックを受け取るメソッドを定義する

More Related Content

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

Rubyist生活に欠かせないブロック構文の話