Skip to content

Instantly share code, notes, and snippets.

@DriftwoodJP
Created December 27, 2017 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DriftwoodJP/5f0421ce0dcbe54726ef114e8dd99a56 to your computer and use it in GitHub Desktop.
Save DriftwoodJP/5f0421ce0dcbe54726ef114e8dd99a56 to your computer and use it in GitHub Desktop.
「アジャイル時代のオブジェクト脳のつくり方 Rubyで学ぶ究極の基礎講座」の7章のソースコード。
# rubocop:disable AsciiComments, EmptyLines, LineLength, Semicolon, VariableNumber, Lambda, Proc, RedundantBlockCall, ShadowingOuterLocalVariable, ShadowedArgument
#
# 「アジャイル時代のオブジェクト脳のつくり方 Rubyで学ぶ究極の基礎講座」の7章のソースコード。
#
#
# lesson 7.2 手続き型と関数型の違い
#
public def plusfive
self + 5
end
[1, 5, 7].map(&:plusfive) # [6, 10, 12]
#
# lesson 7.3 ブロック
#
[1, 2, 3].each { |i| p i } # 1 2 3
[*(1..9)].select { |i| (i % 2).zero? } # 1 4 9
#
# lesson 7.4, 7.5 イテレーター
#
[1, 2, 3].each { |i| p i * i } # [1, 2, 3]
# select ブロックが真を返す要素からなる配列を作成。
[1.1, 2, 3.3, 4].select(&:integer?) # [2, 4]
# reject ブロックが偽を返す要素からなる配列を作成。
[1.1, 2, 3.3, 4].reject(&:integer?) # [1.1, 3.3]
# collect / map ブロックが返す値を集める。
%w[apple orange pineapple strawberry].collect(&:size) # [5, 6, 9, 10]
# find 条件に合う要素を返す。
%w[apple orange pineapple strawberry].find { |i| /^a/ =~ i } # "apple"
# sort_by ブロックの戻り値でソートする。
%w[2 4 13 3 1 10].sort_by(&:to_i) # ["1", "2", "3", "4", "10", "13"]
# grep 「引数 === 要素」である要素を集める。
%w[apple orange pineapple strawberry].grep(/^a/) # ["apple"]
# take_while ブロックが真を返す要素を先頭から取り出して配列を作成。
[*(1..9)].take_while { |i| i < 6 } # [1, 2, 3, 4, 5]
# drop_while ブロックが真を返す要素を先頭から除いて配列を作成。
[*(1..9)].drop_while { |i| i < 6 } # [6, 7, 8, 9]
# reduce / inject ブロックを使って繰り返し計算する。
[1, 5, 7].reduce { |total, n| total + n } # 13
#
# lesson 7.6 関数を受ける関数の作成
#
# Use yield
def arg_one
yield 1
end
arg_one { |x| x + 3 } # 4
def arg_one_twice
yield(1) + yield(2)
end
arg_one_twice { |x| x + 3 } # 9
# Use call method
def arg_one_call(&block)
block.call 1
end
arg_one_call { |x| x + 3 } # 4
# Use Proc.new
plusthree = Proc.new { |x| x + 3 }
# plusthree = proc { |x| x + 3 }
plusthree.call(1) # 4
# Use Kernel#lambda
plusfour = lambda { |x| x + 4 }
# plusfour = ->(x) { x + 4 }
plusfour.call(1) # 5
# Kernel#lambda checks number of arguments.
plusthree.call(1, 2) # 4
# plusfour.call(1, 2) # ArgumentError
#
# lesson 7.7 クロージャー
#
def multi(i)
func = Proc.new { |x| x * 2 }
func.call(i)
end
multi(2) # 4
multi(6) # 12
def multi_with_block(i)
func = Proc.new
func.call(i)
end
multi_with_block(2) { |x| x * 6 } # 12
multi_with_block(6) { |x| x * 8 } # 48
# multi_with_block(8) # ArgumentError
def count
number = 0
func = ->(i) { number += i }
func
end
count.call(1) # 1
count.call(2) # 2
count.call(3) # 3
count.call(4) # 4
fun = count
fun.call(1) # 1
fun.call(2) # 3
fun.call(3) # 6
fun.call(4) # 10
x = 1
func_1 = proc { |x| p x }
func_1.call(3) # 3
p x # 1
func_2 = proc { |y| x = y; p x }
func_2.call(3) # 3
p x # 3
z = 1
func_3 = proc { |y; z| z = y; p z }
func_3.call(3) # 3
p z # 1
def block_example
yield
end
func = proc { p 'Block Example' }
block_example(&func) # "Block Example"
#
# lesson 7.8 ファーストクラスオブジェクト
#
x = lambda { p 'First Class Example' }
x.call # "First Class Example"
y = x
y.call # "First Class Example"
x = lambda { p 'First Class Example' }
def f(x)
x.call
end
f(x) # "First Class Example"
x = lambda {
return lambda { p 'First Class Return' }
}
z = x.call
z.call # "First Class Return"
#
# lesson 7.9 関数合成
#
f = lambda { |x| x + 3 }
g = lambda { |x| x + 8 }
h = lambda { |x| g.call(f.call(x)) }
h.call(2) # 13
# rubocop:enable AsciiComments, EmptyLines, LineLength, Semicolon, VariableNumber, Lambda, Proc, RedundantBlockCall, ShadowingOuterLocalVariable, ShadowedArgument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment