Created
November 18, 2013 01:44
-
-
Save kidach1/7521086 to your computer and use it in GitHub Desktop.
[Ruby] ブロックとProcをちゃんと理解する ref: http://qiita.com/kidachi_/items/15cfee9ec66804c3afd2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ブロックを受け取るメソッドの定義 | |
def give_me_block | |
yield | |
end | |
#メソッドの引数としてブロック(do~end)を渡して、実行 | |
give_me_block do | |
p "Hello, block!" | |
end | |
=> "Hello, block!" #give_me_block内で、yieldによって呼び出された。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#メソッド定義 | |
def give_me_block( &block ) | |
block.call | |
end | |
#実行 | |
give_me_block do | |
p "Hello, block!" | |
end | |
=> "Hello, block!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 1 | |
proc = Proc.new do | |
n = n + 1 | |
end | |
proc.call | |
=>2 | |
proc.call | |
=>3 | |
proc.call | |
=>4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def wonder_seven_box | |
if block_given? | |
yield(7) | |
else | |
p "Don't mind. Feel free to call me." # ブロックが与えられなければこちら | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wonder_seven_box do |x| | |
p 3 + x | |
end | |
=> 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wonder_seven_box do |x| | |
p "happy_block! " * x | |
end | |
=> "happy_block! happy_block! happy_block! happy_block! happy_block! happy_block! happy_block! " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wonder_seven_box | |
=> "Don't mind. Feel free to call me." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def magic_five_box(after_input, someProc) | |
someProc.call(5, after_input) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def wonder_seven_box | |
if block_given? | |
yield(7) | |
else | |
p "Don't mind. Feel free to call me." # ブロックが与えられなければこちら | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def give_me_block( &block ) | |
block.call | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_method ( args ) | |
if args_given? | |
hoge | |
else | |
fuga | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#メソッド定義 | |
def give_me_block( &block ) | |
yield # block.callをやめてyieldに変更 | |
end | |
#実行 | |
give_me_block do | |
p "Hello, block!" | |
end | |
=> "Hello, block!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#メソッド定義 | |
def give_me_block # (&block)を除去 | |
yield | |
end | |
#実行 | |
give_me_block do | |
p "Hello, block!" | |
end | |
=> "Hello, block!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#インスタンス生成 | |
proc1 = Proc.new do | |
p "hoge" | |
end | |
#実行 | |
proc1.call | |
=> "hoge" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#インスタンス生成 | |
proc2 = Proc.new do |s| #sが仮引数 | |
p "Hello, #{s}!" | |
end | |
#実行 | |
proc2.call("Proc") #"Proc"が実引数 | |
=> "Hello, Proc!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#インスタンス生成 | |
lambda1 = lambda { p "hoge" } | |
#実行 | |
lambda1.call | |
=> "hoge" | |
lambda2 = lambda { |s| p "Hello, #{s}!" } | |
lambda2.call("Proc") | |
=> "Hello, Proc!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def magic_five_box(after_input, someProc) | |
someProc.call(5, after_input) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sum_proc = Proc.new do |x, y| | |
p x + y | |
end | |
magic_five_box(3, sum_proc) | |
=> 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string_proc = Proc.new do |x, string| | |
p string * x | |
end | |
magic_five_box('happy_proc! ', string_proc) | |
=> "happy_proc! happy_proc! happy_proc! happy_proc! happy_proc! " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment