C
Csaba Henk
First, the bugreport.
Array.help just gives the following garbage:
---------------------------------------------- Class: TSortArray < Array
(no description...)
------------------------------------------------------------------------
Now the fun part...
By overriding ri's io routines by more flexible (but by default
backward-compatible) version, and adjusting IHelp accordingly, one, if
wants, can get the rendered ri output as string (w/o doing io).
It works as follows:
require 'ihelp-dump'
# Default behaviour is the same, but then...
IHelp.set_dump true
IHelp.dump? # => true
5.help :size # => (the help as a string)
5.help :size, :to => $> # prints the help
5.help :size, :times # => (the concatenation of the two helps)
IHelp.set_dump false # everything works again as it used to
Note that the first help atfer a #set_dump will take some time as IHelp
is re-initialized then.
It's also possible to pass ri options to #set_dump:
IHelp.set_dump true, %w(-f html) # don't use "-T", that's handled by \
IHelp.set_dump false, %w(-f ansi) # #set_dump
And finally, some candy:
IHelp.set_dump true, %w(-f ansi)
open("|less -R","w") { |f| Module.helpall f }
IHelp.set_dump true, %w(-f html)
open("|w3m -T text/html","w") { |f| Module.allhelp f } # #allhelp is the \
# same as #helpall
I guess you can guess what it does...
Note that the latter, dumping all docs in html, can also be done easily
via Ilmari's html renderer as well.
Csaba
Ps. So the code:
require 'ihelp'
require 'rdoc/ri/ri_formatter'
class RI::TextFormatter
@@io = $>
def self.io
@@io
end
def self.io= io
@@io = io
end
def print *a
a.each{|e| @@io << e }
nil
end
def printf *a
@@io << sprintf(*a)
nil
end
def puts *a
a.empty? and a << ""
a.each {|e| @@io << e.chomp + "\n"}
nil
end
end
module IHelp
def self.set_dump boo, args=nil
setargs = proc { args and IHelp::RI_ARGS.clear.concat args }
case boo
when true
@use_stdout.nil? and @use_stdout = ((%w(--no-pager -T) - IHelp::RI_ARGS).size < 2)
setargs[]
IHelp::RI_ARGS << "-T"
@dump=true
when false
unless @use_stdout
%w(--no-pager -T).each {|o| IHelp::RI_ARGS.delete o }
RI::Options.instance.use_stdout = false
end
@use_stdout = nil
setargs[]
RI::TextFormatter.io = $>
@dump = false
else raise ArgumentError, "please give `true' or `false' for first argument"
end
IHelp::RI_ARGS.uniq!
@ri_driver = nil
end
instance_methods.include? "__help" or alias_method :__help, :help
def self.dump?
@dump
end
def help(*args,&b)
if IHelp.dump?
optsa, names = args.partition { |e| e.is_a? Hash }
opts = {}
optsa.each {|h| opts.merge! h}
to = opts[:to] || ""
RI::TextFormatter.io = to
names.empty? and names << nil
names.each {|n| __help n }
to
else __help(*args,&b)
end
end
def self.dumpcheck
@dump or raise "do an `IHelp.setdump true', and then you can come back again"
end
private_class_method :dumpcheck
def allhelp to = ""
IHelp.send :dumpcheck
mod = self
self.is_a? Module or mod = self.class
mhd = mod.help_description
help({:to => to},nil,*((mhd.class_methods + mhd.instance_methods).map { |h|h.name}))
end
alias helpall allhelp
end
Array.help just gives the following garbage:
---------------------------------------------- Class: TSortArray < Array
(no description...)
------------------------------------------------------------------------
Now the fun part...
By overriding ri's io routines by more flexible (but by default
backward-compatible) version, and adjusting IHelp accordingly, one, if
wants, can get the rendered ri output as string (w/o doing io).
It works as follows:
require 'ihelp-dump'
# Default behaviour is the same, but then...
IHelp.set_dump true
IHelp.dump? # => true
5.help :size # => (the help as a string)
5.help :size, :to => $> # prints the help
5.help :size, :times # => (the concatenation of the two helps)
IHelp.set_dump false # everything works again as it used to
Note that the first help atfer a #set_dump will take some time as IHelp
is re-initialized then.
It's also possible to pass ri options to #set_dump:
IHelp.set_dump true, %w(-f html) # don't use "-T", that's handled by \
IHelp.set_dump false, %w(-f ansi) # #set_dump
And finally, some candy:
IHelp.set_dump true, %w(-f ansi)
open("|less -R","w") { |f| Module.helpall f }
IHelp.set_dump true, %w(-f html)
open("|w3m -T text/html","w") { |f| Module.allhelp f } # #allhelp is the \
# same as #helpall
I guess you can guess what it does...
Note that the latter, dumping all docs in html, can also be done easily
via Ilmari's html renderer as well.
Csaba
Ps. So the code:
require 'ihelp'
require 'rdoc/ri/ri_formatter'
class RI::TextFormatter
@@io = $>
def self.io
@@io
end
def self.io= io
@@io = io
end
def print *a
a.each{|e| @@io << e }
nil
end
def printf *a
@@io << sprintf(*a)
nil
end
def puts *a
a.empty? and a << ""
a.each {|e| @@io << e.chomp + "\n"}
nil
end
end
module IHelp
def self.set_dump boo, args=nil
setargs = proc { args and IHelp::RI_ARGS.clear.concat args }
case boo
when true
@use_stdout.nil? and @use_stdout = ((%w(--no-pager -T) - IHelp::RI_ARGS).size < 2)
setargs[]
IHelp::RI_ARGS << "-T"
@dump=true
when false
unless @use_stdout
%w(--no-pager -T).each {|o| IHelp::RI_ARGS.delete o }
RI::Options.instance.use_stdout = false
end
@use_stdout = nil
setargs[]
RI::TextFormatter.io = $>
@dump = false
else raise ArgumentError, "please give `true' or `false' for first argument"
end
IHelp::RI_ARGS.uniq!
@ri_driver = nil
end
instance_methods.include? "__help" or alias_method :__help, :help
def self.dump?
@dump
end
def help(*args,&b)
if IHelp.dump?
optsa, names = args.partition { |e| e.is_a? Hash }
opts = {}
optsa.each {|h| opts.merge! h}
to = opts[:to] || ""
RI::TextFormatter.io = to
names.empty? and names << nil
names.each {|n| __help n }
to
else __help(*args,&b)
end
end
def self.dumpcheck
@dump or raise "do an `IHelp.setdump true', and then you can come back again"
end
private_class_method :dumpcheck
def allhelp to = ""
IHelp.send :dumpcheck
mod = self
self.is_a? Module or mod = self.class
mhd = mod.help_description
help({:to => to},nil,*((mhd.class_methods + mhd.instance_methods).map { |h|h.name}))
end
alias helpall allhelp
end