Module: IRB::IrbLoader

Included in:
ExtendCommand::Load, ExtendCommand::Require, ExtendCommand::Source
Defined in:
lib/irb/ext/loader.rb

Overview

Provides a few commands for loading files within an irb session.

See ExtendCommandBundle for more information.

Constant Summary collapse

ABSOLUTE_PATH_PATTERN =

:nodoc:

case Dir.pwd
when /\A\w:/, /\A#{separator}{2}/
  /\A(?:\w:|#{separator})#{separator}/
else
  /\A#{separator}/
end

Instance Method Summary collapse

Instance Method Details

#absolute_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/irb/ext/loader.rb', line 35

def absolute_path?(path)
  File.absolute_path?(path)
end

#irb_load(fn, priv = nil) ⇒ Object

Loads the given file similarly to Kernel#load

Raises:

  • (LoadError)


27
28
29
30
31
32
# File 'lib/irb/ext/loader.rb', line 27

def irb_load(fn, priv = nil)
  path = search_file_from_ruby_path(fn)
  raise LoadError, "No such file to load -- #{fn}" unless path

  load_file(path, priv)
end

#load_file(path, priv = nil) ⇒ Object

Loads the given file in the current session’s context and evaluates it.

See Irb#suspend_input_method for more information.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/irb/ext/loader.rb', line 98

def load_file(path, priv = nil)
  irb.suspend_name(path, File.basename(path)) do

    if priv
      ws = WorkSpace.new(Module.new)
    else
      ws = WorkSpace.new
    end
    irb.suspend_workspace(ws) do
      FileInputMethod.open(path) do |io|
        irb.suspend_input_method(io) do
          |back_io|
          irb.signal_status(:IN_LOAD) do
            if back_io.kind_of?(FileInputMethod)
              irb.eval_input
            else
              begin
                irb.eval_input
              rescue LoadAbort
                print "load abort!!\n"
              end
            end
          end
        end
      end
    end
  end
end

#oldObject

:nodoc:



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
# File 'lib/irb/ext/loader.rb', line 127

def old # :nodoc:
  back_io = @io
  back_path = @irb_path
  back_name = @irb_name
  back_scanner = @irb.scanner
  begin
    @io = FileInputMethod.new(path)
    @irb_name = File.basename(path)
    @irb_path = path
    @irb.signal_status(:IN_LOAD) do
      if back_io.kind_of?(FileInputMethod)
        @irb.eval_input
      else
        begin
          @irb.eval_input
        rescue LoadAbort
          print "load abort!!\n"
        end
      end
    end
  ensure
    @io = back_io
    @irb_name = back_name
    @irb_path = back_path
    @irb.scanner = back_scanner
  end
end

#search_file_from_ruby_path(fn) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/irb/ext/loader.rb', line 57

def search_file_from_ruby_path(fn) # :nodoc:
  if absolute_path?(fn)
    return fn if File.exist?(fn)
    return nil
  end

  for path in $:
    if File.exist?(f = File.join(path, fn))
      return f
    end
  end
  return nil
end

#source_file(path) ⇒ Object

Loads a given file in the current session and displays the source lines

See Irb#suspend_input_method for more information.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/irb/ext/loader.rb', line 74

def source_file(path)
  irb.suspend_name(path, File.basename(path)) do
    FileInputMethod.open(path) do |io|
      irb.suspend_input_method(io) do
        |back_io|
        irb.signal_status(:IN_LOAD) do
          if back_io.kind_of?(FileInputMethod)
            irb.eval_input
          else
            begin
              irb.eval_input
            rescue LoadAbort
              print "load abort!!\n"
            end
          end
        end
      end
    end
  end
end