SciRuby / iruby

Official gem repository: Ruby kernel for Jupyter/IPython Notebook
https://github.com/SciRuby/iruby
MIT License
901 stars 29 forks source link

Hide output on assignment #312

Closed ankane closed 3 years ago

ankane commented 3 years ago

Hi, this PR hides output on assignment (like Python). This avoids the need to add ; nil to lines. It uses assignment_expression? from IRB and checks that it exists first for older versions (added in https://github.com/ruby/irb/commit/0a3a0f5d9ed543440950dfc2a4ad2ec32e66009e).

Ref: #195

kojix2 commented 3 years ago

In my case, if I don't want it to output, I often put a 0 in the last line. However, I'm neutral on this matter because I don't really care about it. If more people want to change it, it should be changed. What do you think, @mrkn, the owner?

mrkn commented 3 years ago

I have a plan to implement the output prevention by putting single semicolon at the end of the cell. It is the same behavior as IJulia.

kojix2 commented 3 years ago

I found a very interesting issue on IJulia, the kernel of the Julia language. https://github.com/JuliaLang/IJulia.jl/issues/649

In IPython, something like x = np.arange(1000) shows no output, whereas in IJulia x = collect(1:1000) does. I understand this matches the REPL, but is there a way to surpress this by default other than adding semicolons everywhere?

No. When you evaluate a cell in IJulia, it is just returns the value of the last expression, and what you are seeing here is a difference between the Julia and Python languages. In Python, assignment expressions don't have a value, whereas in Julia the assignment x = y has the value y.

In Python and R, assignment expressions do not have a value, but in Julia and Ruby, assignment expressions have a value, so it is more natural to show the value.However, even in the Julia language, there seems to be an argument that the output should be hidden for usability. I was thinking of merging this pull request, but after seeing Julia's policy, I decided to put it on hold again.

mrkn commented 3 years ago

By the way, I want to merge this if this behavior can be opt-in by a configuration like below.

IRuby.silent_assignment = true

@ankane Could you make this configurable? The default configuration should be disable for compatibility.

kojix2 commented 3 years ago

I agree with the semicolon!

mrkn commented 3 years ago

In Julia and Ruby, an assignment is an expression so it has a value, whereas an assignment in Python isn't an expression but a statement so it doesn't have a value.

ankane commented 3 years ago

Added an option and made it opt-in.

mrkn commented 3 years ago

@ankane Thank you so much!

ankane commented 3 years ago

That was fast 🔥 Thanks @mrkn and @kojix2!