File: test_crazy_blocks.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (58 lines) | stat: -rw-r--r-- 945 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'test/unit'

class TestCrazyBlocks < Test::Unit::TestCase
  def foo(a)
    p = proc { a.each {|x| yield x } }
    1.times { p.call }
  end

  def bar(&b)
    [[1,2],[3,4]].each(&b)
  end

  def baz
    bar {|a| foo(a) { |x| yield x } }
  end

  def test_crazy
    a = []
    p = proc {|y| a << y}
    baz {|x| p.call x}
    assert_equal [1,2,3,4], a
  end

  def hello(&b)
    1.times { b.call }
  end

  def test_crazy2
    a = []
    hello {
      p = proc {|y| a << y}
      baz{|x| p.call x}
    }
    assert_equal [1,2,3,4], a
  end

  def test_crazy3
    a = []
    p = proc {|y| a << y}
    self.class.send(:define_method, :goodbye) {
      hello {
        baz {|x| p.call(x)}
      }
    }
    goodbye
    assert_equal [1,2,3,4], a
  end

  def test_crazy4
    a = []
    p = proc {|x| a << x}
    hello {
      p2 = proc {|$x| eval "p.call $x", p}
      baz {|x| eval "p2.call x"}
    }
    assert_equal [1,2,3,4], a
  end
end