Module: Kernel

Defined in:
lib/rmtools/core/kernel.rb

Instance Method Summary collapse

Instance Method Details

#executing?(file = $0) ⇒ Boolean



29
30
31
# File 'lib/rmtools/core/kernel.rb', line 29

def executing? file=$0
  caller[0] =~ /^#{file}:/
end

#obtained(obj, &func) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/rmtools/core/kernel.rb', line 14

def obtained(obj, &func)
  if obj.is Proc
    if obj.arity == 0
      func.call obj.call
    else # obj is a function receiving another function
      obj.call &func
    end
  else func.call obj
  end
end

#require!(file) ⇒ Object

re-require



5
6
7
8
9
10
11
12
# File 'lib/rmtools/core/kernel.rb', line 5

def require!(file)
  [file, File.expand_path(file)].find {|path|
    ['.rb', '.so', '.dll', ''].find {|ext| 
      $".delete "#{file}#{ext}"
    }
  } || $".del_where {|path| path[%r{/#{file}(.rb|.so|.dll)?$}]}
  require file
end

#thread(&block) ⇒ Object



70
71
72
# File 'lib/rmtools/core/kernel.rb', line 70

def thread(&block)
  Thread.new(&block)
end

#TypeError!(given, *expected) ⇒ Object



25
26
27
# File 'lib/rmtools/core/kernel.rb', line 25

def TypeError! given, *expected
  "invalid argument type #{given.class.name}, expected #{expected*' or '}"
end

#whose?(method, *opts) ⇒ Boolean



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
# File 'lib/rmtools/core/kernel.rb', line 33

def whose?(method, *opts)
  opts = *opts.get_opts([:flags], :ns => :public)
  opts[:modules] ||= opts[:mod]
  checker = :"#{opts[:ns]}_method_defined?"
  if Array === method
    methods = method.to_syms
  else 
    methods = [method = method.to_sym]
  end
  if defined? ActiveSupport::JSON::CircularReferenceError
    ActiveSupport::JSON.__send__ :remove_const, :CircularReferenceError
  end
  classes = {}
  
  methods.each {|m|
      classes[m] = {Object => true} if Object.send checker, m and (
        !opts[:modules] or 
        Object.included_modules.select {|mod| (checker[m] ||= {})[mod] = true if mod.__send__ checker, m}.empty?
      )
  }
  methods -= classes.keys
  return classes.map_values {|h| h.keys} if methods.empty?
  
  klass = opts[:modules] ? Module : Class
  ObjectSpace.each_object {|o| 
    methods.each {|m| (classes[m] ||= {})[o] = true if o.name != '' and o.__send__ checker, m} if klass === o
  }
  classes.map_values! {|h| 
    if !opts[:descendants]
      h.each {|c, b| h[c] = nil if (c.is Class and h.key? c.superclass) or (opts[:modules] and c.included_modules.find {|a| h.key? a})}
    end
    h.map {|c,b| b && c}.compact
  }
  
  Array === method ? classes : classes[method]
end