Class: Irbtools::Command::Code

Inherits:
IRB::Command::Base
  • Object
show all
Defined in:
lib/irbtools/commands/code.rb

Instance Method Summary collapse

Instance Method Details

#execute(arg) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/irbtools/commands/code.rb', line 35

def execute(arg)
  if code_parameters_code = transform_arg(arg)
    code_parameters = @irb_context.workspace.binding.eval(code_parameters_code)
    @irb_context.workspace.binding.send(:code, *code_parameters)
  else
    warn "code: Please use rdoc syntax, e.g. Array#sum"
  end
rescue NameError
  warn "code: Class or method not found."
end

#transform_arg(arg) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/irbtools/commands/code.rb', line 17

def transform_arg(arg)
  if arg.empty?
    "[]"
  elsif arg.strip =~ /\A(?:([\w:]+)([#.]))?(\w+[?!]?)\z/
    if $1
      if $2 == "#"
        "[#{$1}, #{$1}.instance_method(:#{$3})]"
      else
        "[#{$1}, :#{$3}]"
      end
    else
      "[:#{$3}]"
    end
  else
    nil
  end
end