Do not confuse return values with STDOUT

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the dumb-mistakes-and-gotchas category.

Last Updated: 2024-03-28

I was confused by some response in a Ruby. When I inspected it, it seemed to have the value 200, but when I performed logic on it, it acted like nil.

The output was like this:

$ test_page_count

200
=> nil

What I should have realized, is that the return value always comes after => in the Ruby REPL (and that it's typically one line)

What confuesd me was that there was an errant puts statement wrapping the code I wanted to execute and puts always returns nil (the wrapped code's return value instead gets printed to STDOUT)

def test_page_count
  puts post('/page_count', file: base_url + '/mistake_framework.pdf')
end

def post
  HTTP.post(base_url + url, form: options).code
end

After removing this puts statement, the output became

$ test_page_count
=> 200