1# 2# loader.rb - 3# $Release Version: 0.9.6$ 4# $Revision: 38515 $ 5# by Keiju ISHITSUKA(keiju@ruby-lang.org) 6# 7# -- 8# 9# 10# 11 12 13module IRB # :nodoc: 14 # Raised in the event of an exception in a file loaded from an Irb session 15 class LoadAbort < Exception;end 16 17 # Provides a few commands for loading files within an irb session. 18 # 19 # See ExtendCommandBundle for more information. 20 module IrbLoader 21 @RCS_ID='-$Id: loader.rb 38515 2012-12-21 05:45:50Z zzak $-' 22 23 alias ruby_load load 24 alias ruby_require require 25 26 # Loads the given file similarly to Kernel#load 27 def irb_load(fn, priv = nil) 28 path = search_file_from_ruby_path(fn) 29 raise LoadError, "No such file to load -- #{fn}" unless path 30 31 load_file(path, priv) 32 end 33 34 def search_file_from_ruby_path(fn) # :nodoc: 35 if /^#{Regexp.quote(File::Separator)}/ =~ fn 36 return fn if File.exist?(fn) 37 return nil 38 end 39 40 for path in $: 41 if File.exist?(f = File.join(path, fn)) 42 return f 43 end 44 end 45 return nil 46 end 47 48 # Loads a given file in the current session and displays the source lines 49 # 50 # See Irb#suspend_input_method for more information. 51 def source_file(path) 52 irb.suspend_name(path, File.basename(path)) do 53 irb.suspend_input_method(FileInputMethod.new(path)) do 54 |back_io| 55 irb.signal_status(:IN_LOAD) do 56 if back_io.kind_of?(FileInputMethod) 57 irb.eval_input 58 else 59 begin 60 irb.eval_input 61 rescue LoadAbort 62 print "load abort!!\n" 63 end 64 end 65 end 66 end 67 end 68 end 69 70 # Loads the given file in the current session's context and evaluates it. 71 # 72 # See Irb#suspend_input_method for more information. 73 def load_file(path, priv = nil) 74 irb.suspend_name(path, File.basename(path)) do 75 76 if priv 77 ws = WorkSpace.new(Module.new) 78 else 79 ws = WorkSpace.new 80 end 81 irb.suspend_workspace(ws) do 82 irb.suspend_input_method(FileInputMethod.new(path)) do 83 |back_io| 84 irb.signal_status(:IN_LOAD) do 85# p irb.conf 86 if back_io.kind_of?(FileInputMethod) 87 irb.eval_input 88 else 89 begin 90 irb.eval_input 91 rescue LoadAbort 92 print "load abort!!\n" 93 end 94 end 95 end 96 end 97 end 98 end 99 end 100 101 def old # :nodoc: 102 back_io = @io 103 back_path = @irb_path 104 back_name = @irb_name 105 back_scanner = @irb.scanner 106 begin 107 @io = FileInputMethod.new(path) 108 @irb_name = File.basename(path) 109 @irb_path = path 110 @irb.signal_status(:IN_LOAD) do 111 if back_io.kind_of?(FileInputMethod) 112 @irb.eval_input 113 else 114 begin 115 @irb.eval_input 116 rescue LoadAbort 117 print "load abort!!\n" 118 end 119 end 120 end 121 ensure 122 @io = back_io 123 @irb_name = back_name 124 @irb_path = back_path 125 @irb.scanner = back_scanner 126 end 127 end 128 end 129end 130 131