Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't echo an expression's result when it ends with a semicolon #669

Merged
merged 1 commit into from
Aug 11, 2023

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Aug 5, 2023

Closes #656

@st0012 st0012 added the enhancement New feature or request label Aug 5, 2023
@st0012 st0012 self-assigned this Aug 5, 2023
class EchoingTest < IntegrationTestCase
def test_irb_echos_by_default
write_ruby <<~'RUBY'
binding.irb
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, opening IRB with IRB.start never works in integration tests.

# this hangs and prints nothing
write_ruby <<~'RUBY'
  require "irb"
  IRB.start(__FILE__)
RUBY

Similarly, introducing a helper like run_irb to make it run exe/irb doesn't work either.

So I decided to go with binding.irb for now and investigate this further later.

@@ -562,7 +562,8 @@ def eval_input
is_assignment = assignment_expression?(line)
evaluate_line(line, line_no)

if @context.echo?
# Don't echo if the line ends with a semicolon
if @context.echo? && !line.match?(/;\s*\z/)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this simple implementation is good enough.

However, I found a false positive. Usingtokens.last will be more strict. What do you think?

puts %;string;
puts +% ; 
puts <<';'
;
puts <<' '
heredoc;
 

And maybe a false negative, maybe not. (Checking this will be very hard)

puts(<<A);
A

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! And yeah I agree it's better to make this check with Ripper. But I'd like to avoid invoking it outside of RubyLex and want to make this property an yield argument of each_top_level_statement instead (like #670). WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think It should be in RubyLex too if it uses ripper.
I'll merge this pull request first and let's use ripper in the followup pull request maybe after #670 👍

@tompng tompng merged commit 50185c2 into master Aug 11, 2023
47 checks passed
@tompng tompng deleted the implement-#656 branch August 11, 2023 16:18
@etiennebarrie
Copy link

Thanks for supporting this! Very useful for example when dealing an ActiveRecord::Relation and not wanting to cause a query to the database.

For what it's worth, Pry's implementation seems limited to ending with a semicolon: a space after the semicolon results in the value being output. I like that this is a bit smarter. Hopefully the false positives can be fixed in the future, though they should be pretty rare. 🤞

st0012 added a commit that referenced this pull request Aug 11, 2023
We already skipped history integration tests in core CI in #675 due to
suspicion on nested IRB sessions don't work on certain operating systems.

But after #669, the evaluation integration test also started to fail on
some Core CI suites. So, it looks like the integration test setup may not
work in Core CI, at least in some suites.

Consider `ruby/irb` already has rather sophisticated test suite, I think
it's better to skip the integration tests in core CI for now.
st0012 added a commit that referenced this pull request Aug 11, 2023
We already skipped history integration tests in core CI in #675 due to
suspicion on nested IRB sessions don't work on certain operating systems.

But after #669, the evaluation integration test also started to fail on
some Core CI suites. So, it looks like the integration test setup may not
work in Core CI, at least in some suites.

Consider `ruby/irb` already has rather sophisticated test suite, I think
it's better to skip the integration tests in core CI for now.
@mykhal
Copy link

mykhal commented May 15, 2024

Nice. Thanks.

Maybe it should be also mentioned in docs somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

Skip inspect when line ends with a semicolon ;
4 participants