Class: IRB::Command::Edit

Inherits:
Base show all
Includes:
RubyArgsExtractor
Defined in:
lib/irb/command/edit.rb

Instance Attribute Summary

Attributes inherited from Base

#irb_context

Instance Method Summary collapse

Methods included from RubyArgsExtractor

#ruby_args, #unwrap_string_literal

Methods inherited from Base

category, description, execute, help_message, #initialize

Constructor Details

This class inherits a constructor from IRB::Command::Base

Instance Method Details

#execute(arg) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/irb/command/edit.rb', line 32

def execute(arg)
  # Accept string literal for backward compatibility
  path = unwrap_string_literal(arg)

  if path.nil?
    path = @irb_context.irb_path
  elsif !File.exist?(path)
    source = SourceFinder.new(@irb_context).find_source(path)

    if source&.file_exist? && !source.binary_file?
      path = source.file
    end
  end

  unless File.exist?(path)
    puts "Can not find file: #{path}"
    return
  end

  if editor = (ENV['VISUAL'] || ENV['EDITOR'])
    puts "command: '#{editor}'"
    puts "   path: #{path}"
    system(*Shellwords.split(editor), path)
  else
    puts "Can not find editor setting: ENV['VISUAL'] or ENV['EDITOR']"
  end
end