Project

General

Profile

Actions

Feature #15724

open

Optionally suppress output from IRB for assignment expressions

Added by onlynone (Steven Willis) over 5 years ago. Updated over 5 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:91937]

Description

Most REPLs I've used don't print out the output of assignment expressions like a = 1. Being able to suppress this output is very useful when you have a string which is essentially a blob, an object with a large string representation, or even an array of several objects like this. This happens to me often with rails' ActiveRecords where a query might return hundreds of objects that I just want to assign to a variable and don't want to inspect yet. It's even worse with rails because queries are run lazily, just because I did a = User.where(name='Sally') doesn't mean I actually wanted to execute that query against the db yet. I might want to do a = a.where('age > 35'). But irb doing the echo forces the evaluation.

I wrote this patch to irb: https://github.com/ruby/irb/pull/12 which would suppress irb's echo for assignment expressions when a new config setting: context.echo_on_assignment is false. It uses the new RubyVM::AbstractSyntaxTree to determine whether an expression is an assignment expression, so it only works on ruby >= 2.6.0. I'd very much welcome some feedback and any ideas on how to get it to work for earlier ruby versions.

I had also written a first-pass proof-of-concept that used the parser gem and would work for ruby >= 2.0.0 (https://github.com/ruby/irb/pull/11). This version obviously can't be merged because of the external dependency, but perhaps it can be treated like readline: use it if it's already installed, but don't depend on it?

Updated by alanwu (Alan Wu) over 5 years ago

You can type a = 1; nil to stop irb from calling inspect on the assignment expression result, just FYI.

Updated by shevegen (Robert A. Heiler) over 5 years ago

While I use a similar approach suggested by Alan Wu - and there is nothing wrong with that - I think
it may still be useful to have a simpler toggle-way for silencing output by default (or, via a toggle).

I don't know if the original suggestion is good as is, but I think it is an interesting idea to support
(unless there is already some config option for irb that does this, but I am not aware of it).

I don't think an external dependency can be merged, though. For enabling additional functionality,
as suggested by readline, I guess this has to be decided by the ruby core team whether that approach
is good; the better approach would obviously be to have this a possibility for everyone. Perhaps
there is another way to find out whether something is an assignment expression - that could actually
be helpful in general. There are a few ruby projects that have to parse input and find out whether
it is valid ruby syntax or not; and then also determine what this token is doing. I have had a similar
need to do so in one project that aimed to write a shell-wrapper for ruby (and then re-use it in
e. g. ruby bindings to vte, or just as a long loop {} to continually use readline to fetch input,
and act on that input).

So from this point of view I think this may be a useful suggestion.

Updated by onlynone (Steven Willis) over 5 years ago

alanwu (Alan Wu) wrote:

You can type a = 1; nil to stop irb from calling inspect on the assignment expression result, just FYI.

Yes, this feature is so that one doesn't have to remember to do that after each expression they wouldn't normally want echo'd.

Updated by onlynone (Steven Willis) over 5 years ago

shevegen (Robert A. Heiler) wrote:

I don't know if the original suggestion is good as is, but I think it is an interesting idea to support
(unless there is already some config option for irb that does this, but I am not aware of it).

The only option is @context.echo which turns all echo-ing on or off.

I don't think an external dependency can be merged, though.

Yes. As mentioned, that version with the parser gem was just my first-pass proof-of-concept to show what could be done. The first link I gave (https://github.com/ruby/irb/pull/12) has no external dependencies, but it's only available for >= 2.6.0.

Perhaps
there is another way to find out whether something is an assignment expression - that could actually
be helpful in general.

That's exactly what I'm hoping the PR and this feature suggestion would help me find out.

Updated by onlynone (Steven Willis) over 5 years ago

Thanks to some feedback on the PR, I've updated it to use Ripper, so it doesn't rely on internals of CRuby (RubyVM) or an external library (parser).

Is there anything else I can do to get this merged?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0