@@ -40,9 +40,11 @@ def CurrentContext # :nodoc:
40
40
end
41
41
42
42
# Initializes IRB and creates a new Irb.irb object at the `TOPLEVEL_BINDING`
43
- def start ( ap_path = nil )
43
+ def start ( ap_path = nil , ** options )
44
44
STDOUT . sync = true
45
45
$0 = File ::basename ( ap_path , ".rb" ) if ap_path
46
+ pre_cmds = options [ :pre ] &.rstrip
47
+ do_cmds = options [ :do ] &.rstrip
46
48
47
49
setup ( ap_path )
48
50
@@ -51,7 +53,7 @@ def start(ap_path = nil)
51
53
else
52
54
irb = Irb . new
53
55
end
54
- irb . run ( @CONF )
56
+ irb . run ( @CONF , pre_cmds : pre_cmds , do_cmds : do_cmds )
55
57
end
56
58
57
59
# Quits irb
@@ -146,7 +148,7 @@ def debug_readline(binding)
146
148
input
147
149
end
148
150
149
- def run ( conf = IRB . conf )
151
+ def run ( conf = IRB . conf , pre_cmds : nil , do_cmds : nil )
150
152
in_nested_session = !!conf [ :MAIN_CONTEXT ]
151
153
conf [ :IRB_RC ] . call ( context ) if conf [ :IRB_RC ]
152
154
prev_context = conf [ :MAIN_CONTEXT ]
@@ -169,6 +171,13 @@ def run(conf = IRB.conf)
169
171
RubyVM . keep_script_lines = true
170
172
end
171
173
174
+ if pre_cmds
175
+ context . evaluate ( parse_input ( pre_cmds ) , @line_no )
176
+ elsif do_cmds
177
+ context . evaluate ( parse_input ( do_cmds ) , @line_no )
178
+ return
179
+ end
180
+
172
181
forced_exit = catch ( :IRB_EXIT ) do
173
182
eval_input
174
183
end
@@ -703,7 +712,7 @@ class Binding
703
712
# Cooked potato: true
704
713
#
705
714
# See IRB for more information.
706
- def irb ( show_code : true )
715
+ def irb ( show_code : true , ** options )
707
716
# Setup IRB with the current file's path and no command line arguments
708
717
IRB . setup ( source_location [ 0 ] , argv : [ ] ) unless IRB . initialized?
709
718
# Create a new workspace using the current binding
@@ -714,6 +723,8 @@ def irb(show_code: true)
714
723
debugger_irb = IRB . instance_variable_get ( :@debugger_irb )
715
724
716
725
irb_path = File . expand_path ( source_location [ 0 ] )
726
+ pre_cmds = options [ :pre ] &.rstrip
727
+ do_cmds = options [ :do ] &.rstrip
717
728
718
729
if debugger_irb
719
730
# If we're already in a debugger session, set the workspace and irb_path for the original IRB instance
@@ -722,14 +733,14 @@ def irb(show_code: true)
722
733
# If we've started a debugger session and hit another binding.irb, we don't want
723
734
# to start an IRB session instead, we want to resume the irb:rdbg session.
724
735
IRB ::Debug . setup ( debugger_irb )
725
- IRB ::Debug . insert_debug_break
736
+ IRB ::Debug . insert_debug_break ( pre_cmds : pre_cmds , do_cmds : do_cmds )
726
737
debugger_irb . debug_break
727
738
else
728
739
# If we're not in a debugger session, create a new IRB instance with the current
729
740
# workspace
730
741
binding_irb = IRB ::Irb . new ( workspace , from_binding : true )
731
742
binding_irb . context . irb_path = irb_path
732
- binding_irb . run ( IRB . conf )
743
+ binding_irb . run ( IRB . conf , pre_cmds : pre_cmds , do_cmds : do_cmds )
733
744
binding_irb . debug_break
734
745
end
735
746
end
0 commit comments