File: testReturn.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 (98 lines) | stat: -rw-r--r-- 1,466 bytes parent folder | download | duplicates (5)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'test/minirunit'
test_check "Test return(s):"

def meth1
  (1..10).each do |val|
    return val
  end
end

test_ok(1 == meth1)

def meth2(&b)
  b
end

res = meth2 { return }

test_exception(LocalJumpError){ res.call }

def meth3
  (1..10).each do |v1|
     ('a'..'z').each do |v2|
        return v2
     end
  end
end

test_ok('a' == meth3)

def meth4
  p = Proc.new { return 99 }
  p.call
  puts "Never get here"
end

test_ok(99 == meth4)

q = Proc.new { return 99 }

def meth5(p)
  p.call
end

test_exception(LocalJumpError) { meth5(q) } 

def meth6
  p = lambda { return 99 }
  test_ok(99 == p.call)
end

meth6

class B
  attr_reader :arr
  def initialize(arr)
    @arr = arr
  end
  def detect (nothing_found = nil)
    z = each { |e| return e if yield(e) }
    # should not get here if return hit
    puts "DOH #{z}"
    nothing_found.call unless nothing_found.nil?
    nil
  end
  def each
    i = 0
    loop do
      break if i == @arr.size
      yield @arr[i]
      i+=1
    end
  end
end


test_ok(2 == B.new([1, 2, 3, 4]).detect {|c| c > 1})

def proc_call(&b)
  b.call
end
def proc_return1
  proc_call{return 42}+1
end
test_ok(proc_return1() == 42)

# Procs (blocks) turn lambda-like when defined as a methods
# (actually define_method instance_eval's them, but lambda is easier for now)
class X
  def self.make_method(sym, &blk)
    define_method(sym, &blk)
  end
  
  make_method :foo do
    return "bar"
  end
end

test_equal("bar", X.new.foo)