Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics (Retired) Ruby Methods Method Returns: Part 2

method return part 2 i am stuck please help!

def mod(a, b) puts "The remainder of #{a} divided by #{b} is #{a % b}" return a % b end puts mod(5, 3)

def mod(a, b)
  puts "The remainder of #{a} divided by #{b}: "

 mod(13, 6)

4 Answers

Nasreldin, you didn't finish the string, nor did you return it:

def mod(a, b)
  #write your code here
  return "The remainder of #{a} divided by #{b} is #{a % b}."  //must be return not puts, needs to show remainder


Thanks very much!

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there! There's a couple of things going on here. First, you never compute the remainder that it's asking for. Now you can do this a couple of ways. You can either set up a variable named c first, or you can send back the calculation directly. But the big thing here lies in the instructions of the challenge. It says to return the string, not print it.

Calculating inside the string:

def mod(a, b)
  return "The remainder of #{a} divided by #{b} is #{a%b}."

Calculating using another variable:

def mod(a, b)
  c = a % b
  return "The remainder of #{a} divided by #{b} is #{c}."

The strings will be identical whichever way you prefer to go. Hope this helps! :smiley:

Thanks very much. I did help!

Patrick, as you know, methods can do different things. They can display text to the console (puts), they can return a value, which the code that calls the method can use, or display, etc.

In this particular case the reasoning for using return rather than puts is that the challenge asked for it: "it would be nicer if the method returned the remainder in a nice full sentence."

Without the return the editor won't accept the code.

Patrick Logan
Patrick Logan
1,151 Points

Using "puts" didn't work. What's the reasoning behind "return" vs. 'puts "The remainder of #{a}..."?