Module: IRT::Commands::Ri

Included in:
IRB::ExtendCommandBundle
Defined in:
lib/irt/commands/ri.rb

Constant Summary collapse

GEM =
case
when IRT::RubyVersion >= '1.9.3' then 'ri'
when IRT::RubyVersion >= '1.9.2' then 'bri'
else 'fastri'
end
@@choices_map =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reset_choices_mapObject



13
14
15
# File 'lib/irt/commands/ri.rb', line 13

def self.reset_choices_map
  @@choices_map = {}
end

Instance Method Details

#pri(arg) ⇒ Object



68
69
70
# File 'lib/irt/commands/ri.rb', line 68

def pri(arg)
  pager { ri arg }
end

#ri(arg, literal = false) ⇒ Object



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
# File 'lib/irt/commands/ri.rb', line 17

def ri(arg, literal=false)
  ensure_session
  raise IRT::NotImplementedError, "No available ri_command_format for this system." unless IRT.ri_command_format
  case

  when arg.match(/^\d+$/)
    if @@choices_map.key?(arg)
      to_search = @@choices_map[arg]
    else
      raise IndexError, "No such method index -- [#{arg}]"
    end

  when literal
    if output = process_ri(arg)
      puts(output)
      return
    end

  when arg =~ /^[A-Z]\w+\#[^.]+/
    to_search = arg

  when arg !~ /\./
    begin
      obj = eval arg, IRB.CurrentContext.workspace.binding
      to_search = obj.class
    rescue NameError
      if output = process_ri(arg)
        puts(output)
        return
      end
      raise
    end

  else
    segm = arg.split('.')
    to_search = segm.pop
    receiver = eval segm.join('.'), IRB.CurrentContext.workspace.binding
    raise NoMethodError, %(undefined method '#{to_search}' for #{receiver.inspect}:#{receiver.class}) \
      unless receiver.respond_to? to_search.to_sym
    meth = receiver.method(to_search.to_sym).inspect
    meth = meth.match(/^\#<Method: (.+)>$/)[1]
    if m = meth.match(/\((.+)\)(.+)/)
      to_search = m[1] + m[2]
    else
      to_search = meth
    end
  end

  puts process_ri(to_search) || 'Nothing found!'
end