Search code examples
rubynullsublimetext

Why does '=> nil' appear in the output when I use this website to code but not when I use Sublime Text?


I'm a beginner studying Ruby and I noticed that when I use this website (https://repl.it/) to code, '=> nil' sometimes appears in the output. However, when I use Sublime Text to code, it does not appear at all.

1) Is it important for it to appear in the output? If so, why?

2) How can I make it so that it does appear in Sublime Text?

Thanks!


Solution

  • nil in this case is just the return value of your method call. It didn't return anything. The repl prints it out for you, but when you run a Ruby file, you won't see it.

    e.g. in pry, irb, repl.it, etc.

    puts 5
    

    prints

    5
    => nil
    

    The puts command prints 5, then returns nil. The repl prints that for you, so you know what the return value was. You can try it for yourself

    def test
      puts 'test'
      return 5
    end
    test
    

    prints

    test
    => 5
    

    If you want it to appear when running a file, you can print the return of the function. e.g.

    puts "=> #{test.inspect}"
    

    result

    test
    => 5
    => nil # this line only if running in repl