1

If I run this

a = "hello"

b = "world"

a
b

in ruby

irb(main):007> a = "hello"
=> "hello"
irb(main):008> b = "world"
=> "world"
irb(main):009> a
irb(main):010> b
=> "world"

in python1

>>> a = "hello"
>>> b = "world"
>>> a
'hello'
>>> b
'world'

Ruby skips the result of a (and if there were more consecutive lines of code it would skip their output too). Ruby only displays the output of the last consecutive command (in this case, b)

Question

Is there a way to force ruby to not do that, that is, if there's >1 consecutive commands, to show the results of all of them, not just the last?


1. Python is just an example, most other scripting languages too.

2
  • 2
    Are you typing those four lines into irb or are you pasting them in? Commented Nov 28, 2024 at 4:01
  • @muistooshort I should clarify, I’m pasting them (technically using vim-slime to ‘send them’ to the irb, but that equates to pasting them)
    – stevec
    Commented Nov 28, 2024 at 4:09

1 Answer 1

6

It's a side effect of multiline editing capability in irb. The whole code block is being treated as a single command and you can edit it as a block (arrow up). There is a configuration to disable it:

# .irbrc
IRB.conf[:USE_MULTILINE] = false

# or
irb --nomultiline

# or
bin/rails c -- --nomultiline

However, there seems to be a caveat that Reline will still treat input as multiline when pasting. You'll need readline-ext installed as well:

gem install readline-ext

https://ruby.github.io/irb/index.html#label-Input+Method

6
  • bin/rails c -- --nomultiline works great locally. Annoyingly, heroku run bin/rails c -- --nomultiline (and many variations I tried) error with /app/vendor/bundle/ruby/3.3.0/gems/thor-1.3.1/lib/thor/base.rb:624:in handle_argument_error': ERROR: "rails console" was called with arguments ["--no-multiline"] (Thor::InvocationError) Usage: "rails console [options]"
    – stevec
    Commented Nov 28, 2024 at 4:48
  • IRB.conf[:USE_MULTILINE] = false sounds promising but didn't seem to affect the rails console locally nor on heroku.
    – stevec
    Commented Nov 28, 2024 at 4:50
  • 1
    @stevec i don't use heroku, so just guessing here, -- needs to be passed to rails c so maybe heroku run "bin/rails c -- --nomultiline" or heroku run -- bin/rails c -- --nomultiline. did you add readline-ext to Gemfile? in the console you can check: Readline should return Readline, if you get Reline it won't work.
    – Alex
    Commented Nov 28, 2024 at 5:03
  • Note on IRB.conf[:USE_MULTILINE] = false. Docs say: > Hash IRB.conf affects the context only once, when the configuration file is interpreted; any subsequent changes to it do not affect the context and are therefore essentially meaningless.
    – pdobb
    Commented Nov 28, 2024 at 14:47
  • 2
    @stevec IRB.conf goes into .irbrc, not every option can be changed after irb starts.
    – Alex
    Commented Nov 28, 2024 at 15:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.