Created
December 31, 2010 17:40
-
-
Save CootCraig/761182 to your computer and use it in GitHub Desktop.
Demo. Proc.new closure. Using call and method 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
# Output | |
# p1_a.call(1) => proc1 - a - 1 | |
# p1_b.call(2) => proc1 - b - 2 | |
# p2_o.call(3) => proc2 - o - 3 | |
# p2_p.call(4) => proc2 - p - 4 | |
# TestClass.m_block(&p1_a) => method TestClass.m_block yield(100) => proc1 - a - 100 | |
# TestClass.m_block(&p1_b) => method TestClass.m_block yield(101) => proc1 - b - 101 | |
# TestClass.m_block(&p2_o) => method TestClass.m_block yield(102) => proc2 - o - 102 | |
# TestClass.m_block(&p2_p) => method TestClass.m_block yield(103) => proc2 - p - 103 | |
class TestClass | |
@@counter = 99 | |
class << self | |
def count | |
@@counter += 1 | |
@@counter | |
end | |
def proc1(create_name) | |
method_name = 'proc1' | |
Proc.new { |arg| | |
"#{method_name} - #{create_name} - #{arg}" | |
} | |
end | |
def proc2(create_name) | |
method_name = 'proc2' | |
Proc.new { |arg| | |
"#{method_name} - #{create_name} - #{arg}" | |
} | |
end | |
def m_block | |
n = TestClass.count | |
"method TestClass.m_block yield(#{n}) => #{yield n}" | |
end | |
end | |
end | |
p1_a = TestClass.proc1 "a" | |
p1_b = TestClass.proc1 "b" | |
p2_o = TestClass.proc2 "o" | |
p2_p = TestClass.proc2 "p" | |
puts "p1_a.call(1) => #{p1_a.call(1)}" | |
puts "p1_b.call(2) => #{p1_b.call(2)}" | |
puts "p2_o.call(3) => #{p2_o.call(3)}" | |
puts "p2_p.call(4) => #{p2_p.call(4)}" | |
puts "TestClass.m_block(&p1_a) => #{TestClass.m_block(&p1_a)}" | |
puts "TestClass.m_block(&p1_b) => #{TestClass.m_block(&p1_b)}" | |
puts "TestClass.m_block(&p2_o) => #{TestClass.m_block(&p2_o)}" | |
puts "TestClass.m_block(&p2_p) => #{TestClass.m_block(&p2_p)}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment