Skip to content

Conversation

@sampersand
Copy link

@sampersand sampersand commented Nov 18, 2025

This PR updates Reline::Unicode.escape_for_print so it explicitly joins with '', instead of $,.

Using $,causes errors in IRB. For example, trying to type puts "hi":

Before

% irb
irb(main):001> $, = 'q'
=> "q"
irb(main):002> pquqtqs "hqi"
hi
=> nqiql

After

% irb
irb(main):001> $, = 'q'
=> "q"
irb(main):002> puts "hi"
hi
=> nil
irb(main):003> 

@sampersand sampersand marked this pull request as ready for review November 18, 2025 23:50
@sampersand
Copy link
Author

cf ruby/irb#1140

@tompng
Copy link
Member

tompng commented Nov 19, 2025

I think there are still more than 10 .join that will be affected by $,.
I'm not really sure if Reline should work with overridden $,.

IRB depends on Reline, RDoc, PP, ReplTypeCompletor, RBS and more. Perhaps all gem's .join needs to be fixed.

# Example of RDoc
irb(main):001> $, = 'foobar'
=> "foobar"
irb(main):002> show_doc Integer
foobarfoobar= foobarIfoobarnfoobartfoobarefoobargfoobarefoobarrfoobar foobar<foobar foobarNfoobarufoobarmfoobarefoobarrfoobarifoobarcfoobarfoobar
foobar
foobarfoobar(from ruby core)foobar
foobarfoobar------------------------------------------------------------------------foobar

Another possible fix will be to set $, to nil while reading input in IRB's code.

# Simplified code of IRB

def with_dollar_comma_nil
    dollar_comma, $, = $, nil
    yield
ensure
  $, = dollar_comma
end

loop do
  input = with_dollar_comma_nil { Reline.readline }
  result = eval input
  with_dollar_comma_nil { pp result }
end

@sampersand
Copy link
Author

sampersand commented Nov 19, 2025

Yeah, when debugging the problem I noticed that there were quite a few .joins, but from my minimal testing it seems like this is the only place where it breaks things? I didn't do anything deeper than just experimenting, but this solves the issues I've been seeing forever (and only recently bothered to investigate!)

We could change IRB instead, do you think that would make more sense?

@tompng
Copy link
Member

tompng commented Nov 22, 2025

it seems like this is the only place where it breaks things?

Actually, all .join are not dead code. I think I can find a set of key-input to produce a problem for almost every .join without argument.
Here are some examples:

# Multiline input
irb(main):001> $, = '[FOOBAR]'
=> "[FOOBAR]"
irb(main):002* [
[FOOBAR]irb(main):003> ]
=> []
# Input "aa", CTRL-a, ALT-c
irb(main):001> $, = '[FOOBAR]'
=> "[FOOBAR]"
irb(main):002> A[foobar]a
# Rendering completion menu
require 'reline'
$, = '[FOOBAR]'
Reline.completion_proc = ->*{['aa', 'ab', 'ac']}
Reline.readline('>')

> # Press "a" and TAB
aa  [FOOBAR]ab  [FOOBAR]ac  [FOOBAR][FOOBAR]...

Changing $, is already deprecated, so it does not make sense to change all standard library to never not use .join().
So if we were to support changing $, in IRB, it should be handled only in IRB.

@tompng tompng closed this Nov 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants