Module: Kernel

Defined in:
lib/arb.rb

Instance Method Summary collapse

Instance Method Details

#arb_import(*modules) ⇒ Object Also known as: import



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/arb.rb', line 4

def arb_import(*modules)
  imported=[]
  tmp=nil
  begin 
    loop do 
      tmp=modules.shift
      break unless tmp
      if require("arb/#{tmp.downcase}")
        imported<<tmp 
      else
        #warn("Fail to require arb/#{tmp}, maybe it had already been required!")
      end
    end
  rescue LoadError=>e
    warn("Fail to load arb/#{tmp}!")
    retry
  end
  imported
end

#arb_use(arb_module_name, prefix = nil, forced = false) ⇒ Object Also known as: use



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
# File 'lib/arb.rb', line 24

def arb_use(arb_module_name,prefix=nil,forced=false)
  prefix="#{prefix}_" if prefix && !prefix.to_s.end_with?('_')
  arb_module_name=arb_module_name.to_s.capitalize.to_sym
  res={:succeed=>[],:failed=>[]}
  tried_arb_use=false
  #always try to import
  arb_import arb_module_name
  return nil unless Arb.constants.include?(arb_module_name)
  arb_module=Module.const_get(:Arb).const_get(arb_module_name)
  Kernel.class_eval do
    arb_module.singleton_methods.each do |m|
      mm=prefix.to_s+m.to_s
      if !forced && Kernel.instance_methods.include?(mm)
        warn("Conflict method: #{mm}")
        res[:failed] << mm
        next
      end
      define_method(mm) do |*args,&block|
        arb_module.send(m,*args,&block)
      end
      res[:succeed] << mm
    end
  end
  res
  #Note that NoMethodError is subclass of NameError, which means that
  #invoking nil.xxx(that throws NoMethodError) will also lead to the
  #Name Error, we have to make sure that e is the instance of NameError!!!
end