Sunteți pe pagina 1din 2

require 'irb/completion'

require 'rbconfig'

module kernel
include config
def r(arg)
(config["target_alias"] == "i386-mswin32") ? ( puts `fri.cmd "#{arg}"` ) :
(puts `fri "#{arg}"`)
end
private :r
end

class array
def filter(&predicate)
self.each do |item|
if(predicate(item))
yield item
end
end
end
end

class object
include config
def puts_ri_documentation_for(obj, meth)
case self
when module
candidates = ancestors.map{|klass| "#{klass}::#{meth}"}
candidates.concat(class << self; ancestors end.map{|k| "#{k}##{meth}"})
else
candidates = self.class.ancestors.map{|klass| "#{klass}##{meth}"}
end
candidates.compact.each do |candidate|
#puts "trying #{candidate}"
desc = (config["target_alias"] == "i386-mswin32") ? ( `fri.cmd
"#{candidate}"` ) : ( `fri "#{candidate}"`)
unless desc == "nil"
# uncomment to use ri (and some patience)
#desc = `ri -t '#{candidate}' 2>/dev/null`
#unless desc.empty?
puts desc
return true
end
end
false
end
private :puts_ri_documentation_for

def method_missing(meth, *args, &block)


if md = /ri_(.*)/.match(meth.to_s)
unless puts_ri_documentation_for(self,md[1])
"ri doesn't know about ##{meth}"
end
else
super
end
end
def ri_(meth)
unless puts_ri_documentation_for(self,meth.to_s)
"ri doesn't know about ##{meth}"
end
end
end

ricompletionproc = proc{|input|
bind = irb.conf[:main_context].workspace.binding
case input
when /(\s*(.*)\.ri_)(.*)/
pre = $1
receiver = $2
meth = $3 ? /\a#{regexp.quote($3)}/ : /./ #}
begin
candidates = eval("#{receiver}.methods", bind).map do |m|
case m
when /[a-za-z_]/; m
else # needs escaping
%{"#{m}"}
end
end
candidates = candidates.grep(meth)
candidates.map{|s| pre + s }
rescue exception
candidates = []
end
when /([a-z]\w+)#(\w*)/ #}
klass = $1
meth = $2 ? /\a#{regexp.quote($2)}/ : /./
candidates = eval("#{klass}.instance_methods(false)", bind)
candidates = candidates.grep(meth)
candidates.map{|s| "'" + klass + '#' + s + "'"}
else
irb::inputcompletor::completionproc.call(input)
end
}
#readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
readline.basic_word_break_characters= " \t\n\\><=;|&"
readline.completion_proc = ricompletionproc

S-ar putea să vă placă și