1assert_equal %q{[1, 2, 3]}, %q{ 2 def getproc &b 3 b 4 end 5 6 def m 7 yield 8 end 9 10 m{ 11 i = 1 12 m{ 13 j = 2 14 m{ 15 k = 3 16 getproc{ 17 [i, j, k] 18 } 19 } 20 } 21 }.call 22} 23assert_equal %q{7}, %q{ 24 def make_proc(&b) 25 b 26 end 27 28 def make_closure 29 a = 0 30 make_proc{ 31 a+=1 32 } 33 end 34 35 cl = make_closure 36 cl.call + cl.call * cl.call 37} 38assert_equal %q{ok}, %q{ 39 class C 40 def foo 41 :ok 42 end 43 end 44 45 def block 46 C.method(:new).to_proc 47 end 48 b = block() 49 b.call.foo 50} 51assert_equal %q{[0, 1, :last, 0, 2, :last]}, %q{ 52 def proc &b 53 b 54 end 55 56 pr = [] 57 proc{|i_b| 58 p3 = proc{|j_b| 59 pr << proc{|k_b| 60 [i_b, j_b, k_b] 61 } 62 } 63 p3.call(1) 64 p3.call(2) 65 }.call(0) 66 67 pr[0].call(:last).concat pr[1].call(:last) 68} 69assert_equal %q{12}, %q{ 70 def iter 71 yield 72 end 73 74 def getproc &b 75 b 76 end 77 78 iter{ 79 bvar = 3 80 getproc{ 81 bvar2 = 4 82 bvar * bvar2 83 } 84 }.call 85} 86assert_equal %q{200}, %q{ 87 def iter 88 yield 89 end 90 91 def getproc &b 92 b 93 end 94 95 loc1 = 0 96 pr1 = iter{ 97 bl1 = 1 98 getproc{ 99 loc1 += 1 100 bl1 += 1 101 loc1 + bl1 102 } 103 } 104 105 pr2 = iter{ 106 bl1 = 1 107 getproc{ 108 loc1 += 1 109 bl1 += 1 110 loc1 + bl1 111 } 112 } 113 114 pr1.call; pr2.call 115 pr1.call; pr2.call 116 pr1.call; pr2.call 117 (pr1.call + pr2.call) * loc1 118} 119assert_equal %q{[1, 2]}, %q{ 120 def proc(&pr) 121 pr 122 end 123 124 def m 125 a = 1 126 m2{ 127 a 128 } 129 end 130 131 def m2 132 b = 2 133 proc{ 134 [yield, b] 135 } 136 end 137 138 pr = m 139 x = ['a', 1,2,3,4,5,6,7,8,9,0, 140 1,2,3,4,5,6,7,8,9,0, 141 1,2,3,4,5,6,7,8,9,0, 142 1,2,3,4,5,6,7,8,9,0, 143 1,2,3,4,5,6,7,8,9,0,] 144 pr.call 145} 146assert_equal %q{1}, %q{ 147 def proc(&pr) 148 pr 149 end 150 151 def m 152 a = 1 153 m2{ 154 a 155 } 156 end 157 158 def m2 159 b = 2 160 proc{ 161 [yield, b] 162 } 163 100000.times{|x| 164 "#{x}" 165 } 166 yield 167 end 168 m 169} 170assert_equal %q{[:C, :C]}, %q{ 171 Const = :top 172 class C 173 Const = :C 174 $pr = proc{ 175 (1..2).map{ 176 Const 177 } 178 } 179 end 180 $pr.call 181} 182assert_equal %q{top}, %q{ 183 Const = :top 184 class C 185 Const = :C 186 end 187 pr = proc{ 188 Const 189 } 190 C.class_eval %q{ 191 pr.call 192 } 193} 194assert_equal %q{1}, %q{ 195 def m(&b) 196 b 197 end 198 199 m{|e_proctest| e_proctest}.call(1) 200} 201assert_equal %q{12}, %q{ 202 def m(&b) 203 b 204 end 205 206 m{|e_proctest1, e_proctest2| 207 a = e_proctest1 * e_proctest2 * 2 208 a * 3 209 }.call(1, 2) 210} 211assert_equal %q{[[], [1], [1, 2], [1, 2, 3]]}, %q{ 212 [ 213 Proc.new{|*args| args}.call(), 214 Proc.new{|*args| args}.call(1), 215 Proc.new{|*args| args}.call(1, 2), 216 Proc.new{|*args| args}.call(1, 2, 3), 217 ] 218} 219assert_equal %q{[[nil, []], [1, []], [1, [2]], [1, [2, 3]]]}, %q{ 220 [ 221 Proc.new{|a, *b| [a, b]}.call(), 222 Proc.new{|a, *b| [a, b]}.call(1), 223 Proc.new{|a, *b| [a, b]}.call(1, 2), 224 Proc.new{|a, *b| [a, b]}.call(1, 2, 3), 225 ] 226} 227assert_equal %q{0}, %q{ 228 pr = proc{ 229 $SAFE 230 } 231 $SAFE = 1 232 pr.call 233} 234assert_equal %q{[1, 0]}, %q{ 235 pr = proc{ 236 $SAFE += 1 237 } 238 [pr.call, $SAFE] 239} 240assert_equal %q{1}, %q{ 241 def m(&b) 242 b 243 end 244 m{1}.call 245} 246assert_equal %q{3}, %q{ 247 def m(&b) 248 b 249 end 250 251 m{ 252 a = 1 253 a + 2 254 }.call 255} 256assert_equal %Q{ok\n}, %q{ 257 class A; def get_block; proc {puts "ok"} end end 258 block = A.new.get_block 259 GC.start 260 block.call 261}, '[ruby-core:14885]' 262 263assert_equal 'ok', %q{ 264 a = lambda {|x, y, &b| b } 265 b = a.curry[1] 266 if b.call(2){} == nil 267 :ng 268 else 269 :ok 270 end 271}, '[ruby-core:15551]' 272 273assert_equal 'ok', %q{ 274 lambda { 275 break :ok 276 :ng 277 }.call 278}, '[ruby-dev:34646]' 279 280assert_equal %q{[:bar, :foo]}, %q{ 281 def foo 282 klass = Class.new do 283 define_method(:bar) do 284 return :bar 285 end 286 end 287 [klass.new.bar, :foo] 288 end 289 foo 290}, "[ ruby-Bugs-19304 ]" 291 292assert_equal 'ok', %q{ 293 $x = :ok 294 def def7(x, y) 295 x[y] 296 $x = :ng 297 end 298 def test_def7 299 def7(lambda {|x| x.call}, Proc.new {return}) 300 $x = :ng 301 end 302 test_def7 303 $x 304}, '[ruby-core:17164]' 305 306assert_equal 'ok', %q{ 307 lambda { a = lambda { return }; $x = :ng; a[]; $x = :ok }.call 308 $x 309}, '[ruby-core:17164]' 310 311assert_equal 'ok', %q{ 312 lambda { a = lambda { break }; $x = :ng; a[]; $x = :ok }.call 313 $x 314}, '[ruby-core:17164]' 315 316assert_equal 'ok', %q{ 317 def def8 318 $x = :ng 319 lambda { a = Proc.new { return }; a[]}.call 320 $x = :ok 321 end 322 def8 323 $x 324}, '[ruby-core:17164]' 325 326 327assert_equal 'ok', %q{ 328 def def9 329 lambda {|a| $x = :ok; a[]; $x = :ng }.call(Proc.new { return }) 330 $x = :ng 331 end 332 def9 333 $x 334}, '[ruby-core:17164]' 335 336assert_equal 'ok', %q{ 337 def def10 338 $x = :ng 339 lambda { 1.times { return } }.call 340 $x = :ok 341 end 342 $x = :ok 343 def10 344 $x 345}, '[ruby-core:17164]' 346 347assert_equal 'ok', %q{ 348 def def11 349 yield 350 end 351 begin 352 lambda { def11 { return } }.call 353 rescue LocalJumpError 354 :ng 355 else 356 :ok 357 end 358}, '[ruby-core:17164]' 359 360assert_equal 'ok', %q{ 361 def def12 362 b = Proc.new { $x = :ng; lambda { return }.call; $x = :ok }.call 363 end 364 def12 365 $x 366}, '[ruby-core:17164]' 367 368assert_equal 'ok', %q{ 369 def m 370 pr = proc{ 371 proc{ 372 return :ok 373 } 374 }.call 375 pr.call 376 :ng 377 end 378 m() 379} 380 381assert_equal 'ok', %q{ 382 class Foo 383 def call_it 384 p = Proc.new 385 p.call 386 end 387 end 388 389 def give_it 390 proc { :ok } 391 end 392 393 f = Foo.new 394 a_proc = give_it 395 f.call_it(&give_it()) 396}, '[ruby-core:15711]' 397 398assert_equal 'foo!', %q{ 399 class FooProc < Proc 400 def initialize 401 @foo = "foo!" 402 end 403 404 def bar 405 @foo 406 end 407 end 408 409 def bar 410 FooProc.new &lambda{ 411 p 1 412 } 413 end 414 415 fp = bar(&lambda{ 416 p 2 417 }) 418 419 fp.bar 420}, 'Subclass of Proc' 421 422assert_equal 'ok', %q{ 423 o = Object.new 424 def o.write(s); end 425 $stderr = o 426 at_exit{ 427 print $!.message 428 } 429 raise "ok" 430} 431 432assert_equal 'ok', %q{ 433 lambda do 434 class A 435 class B 436 proc{return :ng}.call 437 end 438 end 439 end.call 440 :ok 441} 442 443assert_equal 'ok', %q{ 444 $proc = proc{return} 445 begin 446 lambda do 447 class A 448 class B 449 $proc.call 450 end 451 end 452 end.call 453 :ng 454 rescue LocalJumpError 455 :ok 456 end 457} 458 459assert_equal 'ok', %q{ 460 def x 461 binding 462 end 463 b = x{|a| a } 464 b.eval('yield("ok")') 465}, '[Bug #5634]' 466 467assert_equal 'ok', %q{ 468 def x 469 binding 470 end 471 eval("x { 'ok' }").eval "yield" 472}, '[Bug #5634]' 473 474assert_equal 'ok', %q{ 475 def x 476 binding 477 end 478 def m 479 x{ 'ok' } 480 end 481 eval('yield', m) 482}, '[Bug #5634]' 483 484