GithubHelp home page GithubHelp logo

programming-univbasics-3-method-return-values-nyc-web-030920's Introduction

Return Values

Learning Goals

  • Identify implicit return values in Ruby syntax
  • Recognize the explicit return keyword

Introduction

As you learned when studying expressions, Ruby returns a value when it evaluates an expression. This is called, suitably, the return value of an expression.

Methods also have return values. Let's study how they work.

Identify Implicit Return Values in Ruby Syntax

Ruby uses something called implicit return which means that the last expression in a method's implementation is the return value of the method itself.

def a_method(a, b)
  puts "hi"
  a + b
end

a_method(1,2) #=> 3

In the implementation, we didn't have to assign the return value to a variable. All you have to do is put an expression right before the end and ruby will evaluate it. In other programming languages like JavaScript you must explicitly say:

{
 ...
 return 1 + 2
}

But Ruby likes for you to be able to type less and realizes that in most methods there's a return value. So you don't have to type the word return. Yay!

Be very careful. Recall that the return value of puts and print is nil.

A VERY COMMON BUG FOR NEW RUBYISTS IS THE FOLLOWING:

def a_method(a,b)
  puts "I got #{a}"
  puts "I got #{b}"
  sum = a + b
  puts "I got #{sum}"
end

a_method(2,3) #=> nil (<==== Surprising?!)


# Prints:
# I got 2
# I got 3
# I got 5

It's very easy to see the error here, especially if you pay attention to the return values of your expressions. The return value of puts is always nil! Because Ruby methods always "return" the return value of the last expression ( or whatever you explicitly return with the return keyword), this method, which is returning a sum, actually returns nil.

To avoid this bug, our code would have to look like:

def a_method(a,b)
  puts "I got #{a}"
  puts "I got #{b}"
  sum = a + b
  puts "I got #{sum}"
  sum
end

a_method(2,3) #=> 5 (<=== What we expect)

# Prints:
# I got 2
# I got 3
# I got 5

It flies against millennia of human evolution, but what you see is not always what is being returned. Put another way: Seeing something printed on the screen by a method is no guarantee that it is the return value of that method.

Code Return Value
"Hello world" "Hello world"
6 + 3 9
instructor = "Tim" "Tim"
total = 5 + 4 9
puts "hello world" nil
print "hello world" nil

Moment for Meta-Learning: If these return values are surprising or don't make sense, test things out in IRB and / or review lessons in Programming as Conversation Part 1.

As you might recall from Programming as Conversation 2: p prints as well as returns the input. That might be the right tool, depending on your situation.

Recognize the Explicit return Keyword

Speaking of explicit return like in JavaScript (and Java, and Python for that matter!) Ruby does have an explicit return command. Rubyists typically use it to exit early from a method with a specific return value.

Let's take a look:

def stylish_chef
  best_hairstyle = "Guy Fieri"
  return "Martha Stewart"
  "Guy Fieri"
end

What do you expect the return value of the above method to be? Using IRB, copy and paste the above method and call it.

You may have expected the return value to be "Guy Fieri". His name is the last line of the method. However, the return value of the above method is actually => Martha Stewart!

The return keyword will disrupt the execution of your method. If you employ it, your method will return whatever you have explicitly told it to (in this case, "Martha Stewart"), and then stop the remainder of the lines won't even be seen!

The explicit use of the return keyword is generally avoided by many Rubyists, but there are instances where you might want to use return instead of relying on implicit returns. What if you decided that a slow calculation could be avoided if a simple condition were true. This pattern is known as a "guard clause" if you want to be very stylish. It helps you avoid an if...else...end conditional.

def get_stock_market_data(date)
  return nil if is_a_weekend?(date)
  # Imagine an expensive, slow calculation hereafter
end

To see the value of this, we'll need to learn more about collection data types, but that's the subject of our next module. Additionally, sometimes the return values of some of the basic Ruby methods might return something that is unpredictable. Using return explicitly lets you make sure your method always returns what you need.

Conclusion

Knowing how methods return values is crucial as you'll be using them constantly in programs both big and small. Knowing the difference between puts and print, and return values will help you avoid a common pitfall.

Return values are how different parts of your program communicate with one another. You don't have to worry too much about this for now, but as you start to build more complicated programs, you'll find that the return value of one method might be operated on by a subsequent method. Don't forget to watch out for those nils!

Resources

programming-univbasics-3-method-return-values-nyc-web-030920's People

Contributors

maxwellbenton avatar drakeltheryuujin avatar sgharms avatar

Watchers

 avatar  avatar Victoria Thevenot avatar Belinda Black avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.