Sha256: 9751026b36833a270a93c2e21ffc4df4af1033962ceee11949878d0698ad7ee3
Contents?: true
Size: 1.21 KB
Versions: 1
Compression:
Stored size: 1.21 KB
Contents
require "irb/command" module Irbtools module Command class Code < IRB::Command::Base category "Introspection" description "Shows the syntax-highlighted source code of a method" help_message <<~HELP Shows the syntax-highlighted source code of a method. Works with Ruby's native methods. Example usages: >> code SecureRandom.uuid >> code Array#reverse HELP 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 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 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
irbtools-4.1.0 | lib/irbtools/commands/code.rb |