Class: AskChatgpt::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/ask_chatgpt/helpers.rb

Defined Under Namespace

Classes: DummyInputMethod

Class Method Summary collapse

Class Method Details

.extract_source(method_or_class_or_str) ⇒ Object



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

def extract_source(method_or_class_or_str)
  case method_or_class_or_str
  when ::Proc, ::Method, ::UnboundMethod
    method_or_class_or_str.source
  when Module, Class, String
    str = capture_io do
      init_irb unless IRB.CurrentContext
      IRB.CurrentContext.main.irb_show_source(method_or_class_or_str.to_s)
    end.join("\n")
    # check if source was extracted
    if str.include?("locate a definition for")
      method_or_class_or_str.to_s
    else
      str
    end
  else
    raise "Not supported parameter"
  end
end