Project

General

Profile

Bug #3143 ยป easy_seg_fault.rb

Ruby file showing the bug - danielribeiro (Daniel Ribeiro), 04/14/2010 12:01 AM

 
1
#!/usr/bin/env ruby
2
class Module
3
  public :include
4
end
5

    
6
class Object
7
  def metaclass
8
    class << self; self; end
9
  end
10
end
11

    
12
class A
13
  def hi(*args, &block)
14
    puts "from A: the args are #{args} and the block is #{block}"
15

    
16
    'just hi'
17
  end
18
end
19

    
20

    
21
module ExtensionModule
22
  def self.create
23
    return Module.new.extend(self)
24
  end
25

    
26

    
27
  def create_blockcall
28
    ## Commenting the following two lines the seg fault turn into  `new': stack level too deep
29
    regex = /#<Module\:0x(\w+)/
30
    uniquename = regex.match(to_s)[1]
31
    ##
32

    
33
    return "blockcall_#{object_id}"
34
  end
35

    
36
  def define_inline_block(name, &blk)
37
    blockcallmethod = create_blockcall
38
    define_method(blockcallmethod, &blk)
39
    evalstr =<<modeval
40
def #{name}(*args, &block)
41
  #{blockcallmethod} *args, &block
42
  super
43
end
44
modeval
45
    self.module_eval evalstr, "meta_#{__method__}" + __FILE__, __LINE__
46
    return self
47
  end
48
end
49

    
50
class Class
51
  alias oldnew new
52

    
53
  def new
54
    return oldnew unless @names
55
    ret = oldnew
56
    for m in @names
57
      ret.extend(m)
58
    end
59
    return ret
60
  end
61

    
62
  def pre_with_block(name, &block)
63
    @names ||= []
64
    @names << ExtensionModule.create.define_inline_block(name, &block)
65
  end
66

    
67
end
68

    
69

    
70

    
71
class A
72

    
73
  pre_with_block :hi do |*args|
74
    puts "invoked by a block with args = '#{args}'"
75
  end
76
end
77

    
78

    
79
puts 'ok'