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

James Matthews
James Matthews
21,610 Points

Method Returns Part II Challenge?

What is the faults(s) in this code in the challenge? It's telling me to "return a nicely formatted string".

method.rb
def mod(a, b)
  puts "Dividing #{a} by #{b}:" 
  return a % b
end

1 Answer

Hi James,

You need to return a string that says, β€œThe remainder of a divided by b is c.” where the variable values are inserted. For this you don't want to use the puts keyword as that outputs the string. You want to return it. First, though, you must calculate the value of c. You can either do that by creating a new variable (as I have done below) or you can just 'do the maths' inside the interpolation like #{a % b} - I prefer the new variable as it is clearer to me; personal preference.

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

You could omit the keyword return as Ruby returns the last evaluated line in a method. But I prefer it there; it makes it clearer. Again, personal preference.

I hope that helps.

Steve.