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

Ethan Klein
Ethan Klein
733 Points

c is a % b

I'm currently having trouble finishing this code while only having two arguments instead of three.

method.rb
def mod(a, b)
  return ("The remainder of #{a} divided by #{b} is #{c}.");
  puts a % b
end
mod(100, 5)

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

To add to Andren's answer ...

You could also eliminate the c = a % b line and do the equation right inside of the interpolation.

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

:dizzy:

Ethan Klein
Ethan Klein
733 Points

Thank you for the reply. Putting it in the interpolation is very clean. :)

andren
andren
28,558 Points

You aren't limited to only using passed in arguments in a function, you can create your own variables. You know that c is meant to hold a % b so you can just create it yourself:

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

You also don't need to print anything with puts to pass this task. And for the record anything below a return statement is never executed anyway, since the function stops once a return is hit.

Note that I also removed the parentheses and semicolon from your return statement, as they are not necessary.