One Thing I Didn't Understand When I Started Learning Ruby

Posted by danikrist on May 26, 2017

Sometimes things click. Sometimes they don’t.

When I started going through my first Ruby lessons, there were one thing I didn’t understand that tripped up many of my labs.

Return

The lab instructions say to “return true or false,” so my code should look like this, right?

def my_method(arg)
  if my_array.include?(arg)
	  return true
	else
	  return false

But the instructions say “return,” I’m so confused!

If I don’t use return how do I return something? By writing nothing.

What?!? Replace return in your code with nothing.

def my_method(arg)
  if my_array.include?(arg)
	  true
	else
	  false

Implicit returns! Simple enough to be confusing.

The Ruby Style Guide - return