32
2014 7 26 Ruby 初級者向けレッスン 50 ブロック ひがき @ Ruby 関西

Ruby初級者向けレッスン 50回 ─── ブロック

  • Upload
    higaki

  • View
    488

  • Download
    1

Embed Size (px)

DESCRIPTION

P.26 「ブロックは Proc」は間違いでした。

Citation preview

Page 1: Ruby初級者向けレッスン 50回 ─── ブロック

2014年7月26日

Ruby初級者向けレッスン 50回— ブロック —

ひがき @ Ruby関西

Page 2: Ruby初級者向けレッスン 50回 ─── ブロック

お品書き• ブロックとは?

– 繰り返し– Enumerable

• メソッドにブロックを渡す• ブロックで値を受け取る• メソッドでブロックを受け取る• ブロックに値を渡す

Page 3: Ruby初級者向けレッスン 50回 ─── ブロック

繰り返しa = [0, 1, 2]

a.each do |i|

puts i

end

# >> 0

# >> 1

# >> 2

Page 4: Ruby初級者向けレッスン 50回 ─── ブロック

繰り返しa = [0, 1, 2]

a.each{|i| puts i}

# >> 0

# >> 1

# >> 2

Page 5: Ruby初級者向けレッスン 50回 ─── ブロック

Enumerable• 繰り返しを行なうクラスのためのモジュール• クラスには each メソッドが必要

Array.ancestors

# => [Array, Enumerable, Object, Kernel, BasicObject]

Hash.ancestors

# => [Hash, Enumerable, Object, Kernel, BasicObject]

Page 6: Ruby初級者向けレッスン 50回 ─── ブロック

BasicObject

Object

4

4

�Mix-in�Kernel

�Mix-in�Enumerable

4

mapselect

inject

Array

Hash

each

each

4

Page 7: Ruby初級者向けレッスン 50回 ─── ブロック

便利な例a = [0, 1, 2, 3] # => [0, 1, 2, 3]

a.map{|i| i * i} # => [0, 1, 4, 9]

a.select{|i| i.even?} # => [0, 2]

a.inject{|s, i| s + i} # => 6

a.find{|i| i.odd?} # => 1

a.all?{|i| i.even?} # => false

a.any?{|i| i.even?} # => true

Page 8: Ruby初級者向けレッスン 50回 ─── ブロック

array.map{|i| i * i}アプリケーション ブロック オブジェクト

-生成 �� ��{|i| i * i}-map

� 呼出

Page 9: Ruby初級者向けレッスン 50回 ─── ブロック

ブロックを渡す• メソッドには、ブロックをひとつ渡せる。• ブロックをどう使うかは、メソッド次第。

open(’hello.txt’) # => #<File:hello.txt>

open(’hello.txt’){|f| f.read}

# => "こんにちは\n"

Page 10: Ruby初級者向けレッスン 50回 ─── ブロック

ファイル入出力の例begin

f = open(’hello.txt’)

f.read

ensure

f.close unless f.nil?

end

Page 11: Ruby初級者向けレッスン 50回 ─── ブロック

ブロックのない openアプリケーション オブジェクト

-open

-read

-close

×

Page 12: Ruby初級者向けレッスン 50回 ─── ブロック

ブロック付き openopen(’hello.txt’){|f| f.read}

アプリケーション ブロック オブジェクト

�� ��{|f| f.read}

-生成

-open

� 呼出

�close

×

Page 13: Ruby初級者向けレッスン 50回 ─── ブロック

値を受け取る• ブロックは、値を受け取れる。• 何を幾つ受け取れるかは、メソッド次第。

Page 14: Ruby初級者向けレッスン 50回 ─── ブロック

値は受け取らなくてもいい2.times{|i| puts i}

# >> 0

# >> 1

2.times{puts ’こんにちは’}

# >> こんにちは# >> こんにちは

Page 15: Ruby初級者向けレッスン 50回 ─── ブロック

Hash の例people = {matz: 49, dhh: 34}

# => {:matz=>49, :dhh=>34}

people.each{|person| p person}

# >> [:matz, 49]

# >> [:dhh, 34]

Page 16: Ruby初級者向けレッスン 50回 ─── ブロック

Hash の例 (2)

people = {matz: 49, dhh: 34}

people.each do |name, age|

puts "#{name}(#{age})"

end

# >> matz(49)

# >> dhh(34)

Page 17: Ruby初級者向けレッスン 50回 ─── ブロック

each cons の例midosuji = ["梅田", "淀屋橋",

"本町", "心斎橋", "なんば"]

midosuji.each_cons(2){|path| p path}

# >> ["梅田", "淀屋橋"]

# >> ["淀屋橋", "本町"]

# >> ["本町", "心斎橋"]

# >> ["心斎橋", "なんば"]

Page 18: Ruby初級者向けレッスン 50回 ─── ブロック

each cons の例 (2)

midosuji.each_cons(2) do |from, to|

p "#{from} - #{to}"

end

# >> "梅田 - 淀屋橋"

# >> "淀屋橋 - 本町"

# >> "本町 - 心斎橋"

# >> "心斎橋 - なんば"

Page 19: Ruby初級者向けレッスン 50回 ─── ブロック

each cons の例 (3)

a = [*0..3] # => [0, 1, 2, 3]

a.each_cons(3){|i| p i}

# >> [0, 1, 2]

# >> [1, 2, 3]

a.each_cons(3){|i, j| p [i, j]}

# >> [0, 1]

# >> [1, 2]

Page 20: Ruby初級者向けレッスン 50回 ─── ブロック

ブロックを受け取るメソッド• こんな感じで呼びたい

monta{puts ’block!’}

# >> block!

# >> block!

# >> 大切なことなので

Page 21: Ruby初級者向けレッスン 50回 ─── ブロック

ブロックを受け取るdef monta

yield

yield

puts ’大切なことなので’

end

Page 22: Ruby初級者向けレッスン 50回 ─── ブロック

ブロックを受け取る (2)

def monta(&block)

block.call

block.call

puts ’大切なことなので’

end

Page 23: Ruby初級者向けレッスン 50回 ─── ブロック

値を渡すdef monta

yield ’大切なことなので’

yield ’大切なことなので’

end

monta{|i| puts i}

# >> 大切なことなので# >> 大切なことなので

Page 24: Ruby初級者向けレッスン 50回 ─── ブロック

値を渡す (2)

def monta(&block)

block.call ’大切な’, ’ことなので’

block.call [’大切な’, ’ことなので’]

end

monta{|i| p i}

# >> "大切な"

# >> ["大切な", "ことなので"]

Page 25: Ruby初級者向けレッスン 50回 ─── ブロック

値を渡す (3)

def monta(&block)

block.call ’大切な’, ’ことなので’

block.call [’大切な’, ’ことなので’]

end

monta{|i, j| p "#{i}, #{j}"}

# >> "大切な, ことなので"

# >> "大切な, ことなので"

Page 26: Ruby初級者向けレッスン 50回 ─── ブロック

ブロックは Procblock = Proc.new do |i, j|

puts "#{i}#{j}"

end

monta(&block)

# >> 大切なことなので# >> 大切なことなので

Page 27: Ruby初級者向けレッスン 50回 ─── ブロック

演習問題 0今日のレッスンで分からなかったこと、疑問に思ったことをグループで話し合ってみよう。

Page 28: Ruby初級者向けレッスン 50回 ─── ブロック

演習問題 10 から 9 までの数値をもつ配列 a がある。

• 各要素を順番に表示しよう。• 各要素を 2倍した値を持つ配列を作ろう。• 全要素の合計値を計算しよう。

a = (0..9).to_a

a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Page 29: Ruby初級者向けレッスン 50回 ─── ブロック

演習問題 20 から 9 までの数値をもつ配列 a がある。奇数の要素だけを持つ配列を作ろう。ただし odd? メソッドは使用禁止。

Page 30: Ruby初級者向けレッスン 50回 ─── ブロック

演習問題 3Enumerable#map を自作してみよう。

module Enumerable

def my_map

……end

end

ただし Enumerable#map と Enumerable#map!

は使用禁止。

Page 31: Ruby初級者向けレッスン 50回 ─── ブロック

自己紹介• 名前 (ニックネーム)

• 普段の仕事・研究内容・代表作• Ruby歴・コンピュータ歴• 勉強会に来た目的• などなど

Page 32: Ruby初級者向けレッスン 50回 ─── ブロック

参考• 公式サイトhttps://www.ruby-lang.org/

• るりまhttp://docs.ruby-lang.org/ja/

• 解答例https://github.com/higaki/

learn_ruby_kansai_62