Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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)}"
Add Comment
Please, Sign In to add comment