Bug #17564


Source command in irb exits after reading and evaluating the file

Added by rsharman (Richard Sharman) almost 2 years ago. Updated almost 2 years ago.

Target version:
ruby -v:
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin17]


In the irb of ruby 2.3.7 (the version that comes with Macos) the “source“ command allows ruby code to be fed to irb as if it were typed in by hand. This has a advantage over the “load” command because variables can be setup not just functions.

However, for ruby versions 2.7.2 and 3.0 irb’s “source” command exits after reading and evaluating everything. This is not very useful in an interactive environment.

I found what I think was a crucial difference between version 2.3.7 and later versions: in function "each_top_level_statement" of irb/ruby-lex.rb the statement

  	break unless l

was replaced by

	break if @io.eof?

When I replaced “break if @io.eof?” by “break unless l” in version 3.0.0 the old behaviour of the “source” command returned! I can now read in ruby code, including variables, and use them.

See file patch for a unified diff of the change.

I don’t understand why this “fix” works, and realize it may not be valid, but I am submitting it in case it is legitimate and can be used.

A trivial file demonstrates the problem: file “a.rb” simply sets variable “a”. To test, invoke irb and issue the command “source ‘a.rb’”. The expected behaviour is you are left at the irb prompt and variable a is valid. The behaviour in verions 3.0.0 without the fix is irb exits.

See file a.rb and output with and without fix in file Log.


a.rb (9 Bytes) a.rb In irb, source this file rsharman (Richard Sharman), 01/19/2021 11:39 PM
Log (298 Bytes) Log Output before and after the patch rsharman (Richard Sharman), 01/19/2021 11:41 PM
Patch (383 Bytes) Patch Diff -u of lib/ruby/3.0.0/irb/ruby-lex.rb rsharman (Richard Sharman), 01/19/2021 11:42 PM

Updated by mame (Yusuke Endoh) almost 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to aycabta (aycabta .)

Updated by no6v (Nobuhiro IMAI) almost 2 years ago

I could reproduce the situation and I made a pull-request to fix this:

Unexpectedly, there are many changes, but most of the measures are to pass existing tests and prevent file leaks.

Actions #3

Updated by no6v (Nobuhiro IMAI) almost 2 years ago

  • Status changed from Assigned to Closed

Applied in changeset git|e80e5a2f897088bc5284ea61817a910d1d334652.

[ruby/irb] use RubyLex::TerminateLineInput appropriately [Bug #17564]

  • using the appropriciate exception instead of break so that the session
    can be continue after the irb_source and irb_load commands
  • suppress extra new line due to one more #prompt call


Also available in: Atom PDF