File: init.rb

package info (click to toggle)
dlr-languages 20090805%2Bgit.e6b28d27%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 51,484 kB
  • ctags: 59,257
  • sloc: cs: 298,829; ruby: 159,643; xml: 19,872; python: 2,820; yacc: 1,960; makefile: 96; sh: 65
file content (99 lines) | stat: -rw-r--r-- 1,652 bytes parent folder | download
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
99
require 'mock'

class Array
  def init *a, &b
    initialize *a, &b
  end
end

puts '-- new ----------------------------------------'

class C
end

p Array.new()
p Array.new(I.new(1))
p Array.new(A.new([1,2]))
p Array.new(AI.new([1,2], 1))

puts '----'

p Array.new(I.new(5), C.new)
p Array.new(A.new([1,2]), C.new) rescue p $!

puts '----'

p Array.new(I.new(5)) { |x| x + 10 }
p Array.new(A.new([1,2,3])) { |x| raise }
p Array.new(AI.new([1,2,3], 10)) { |x| raise }

puts '----'

p Array.new(5) { break 'break result' }

[false, true].each do |$freeze|

    puts "-- initialize (frozen = #$freeze) -------"

	def test_initialize *a, &b
	  array = ['o','r','i','g','i','n','a','l']
	  
	  if $freeze
	    array.freeze 
	    (r = array.init(*a, &b)) rescue p $!
	  else
	    r = array.init(*a, &b)
	  end  
	  p r, array
	end
	
	test_initialize()
	test_initialize(I.new(1))
	test_initialize(A.new([1,2]))
	test_initialize(AI.new([1,2], 1))
	
	puts '----'
	
	test_initialize(I.new(5), C.new)
	test_initialize(A.new([1,2]), C.new) rescue p $!
	
	puts '----'
	
	test_initialize(I.new(5)) { |x| x + 10 }
	test_initialize(A.new([1,2,3])) { |x| raise }
	test_initialize(AI.new([1,2,3], 10)) { |x| raise }
	
	puts '----'
	
	p test_initialize(5) { break 'break result' }

end

puts '-----------------------------------------------'

class Class
  alias xnew new
  alias xallocate allocate
  
  def new *a, &b
    puts 'Class#new'
    p a,b
    xnew *a, &b   
  end
  
  def allocate *a, &b
    puts 'Class#allocate'
    p a,b
    xallocate *a, &b   
  end
end

class MA < Array
end

p a = MA[1,2,3]

class Class
  alias new xnew
  alias allocate xallocate
end