Method: MethodExtensions::MethodSourceWithDoc#irb_inspect

Defined in:
lib/method_extensions/method/source_with_doc.rb

#irb_inspectObject

Place this to the ~/.irbrc class Method

alias_method :inspect, :irb_inspect if method_defined?(:irb_inspect)

end class UnboundMethod

alias_method :inspect, :irb_inspect if method_defined?(:irb_inspect)

end



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/method_extensions/method/source_with_doc.rb', line 121

def irb_inspect
  return to_s if caller.grep(/pp\.rb/) # pretty_print has some logic
  # based on output of Method#inspect, nasty, huh?

  require "coderay"
  require "rdoc/ri/driver"
  
  puts to_s

  if source_location
    puts "#{ source_location[0] }:#{ source_location[1] }"
  end

  d = doc

  if d && !(d = d.chomp).empty?
    # puts
    # TODO
    # 1. is there better way to drive RDoc?
    # 2. probably need to detect other doc types?
    # 3. RDoc::Markup::ToAnsi formatter reflows to the 78 chars width which
    #    looks ugly
    # 
    #  begin
    #    formatter = Class.new(RDoc::Markup::ToAnsi) do
    #      def wrap(text)
    #        @res << text
    #      end
    #    end.new
    #    parser = Class.new.extend(RDoc::Text)
    #    d = d.split("\n").map { |l| parser.parse(l).accept(formatter) }.join("\n")
    ##        rescue Exception => e
    #    puts e.inspect
    #    # anything goes wrong - just display comment verbatim
    #  end
    print d
    # puts
  end

  begin
    src = source
    if src && !src.empty?
      puts CodeRay.scan(source_unindent(src), :ruby).term
    end
  rescue ArgumentError => e
    puts e.message
  end
  
  nil
end