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

Shawn Vogler
Shawn Vogler
564 Points

How do I interpolate a return value in a method?

"In the previous challenge, we wrote a method that returned the remainder of two arguments when divided. That's cool, but it would be nicer if the method returned the remainder in a nice full sentence. Use string interpolation to return the remainder inside the sentence “The remainder of a divided by b is c.” where a is your “a” variable, b is your “b” variable, and c is the value of a % b."

So I understand interpolating a string using #{ }. But I don't understand how to get the remainder value set to "c" Here's the coded that I have so far:

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

mod (10), (7)

c = a % b

Should C be one of the variables in the method? Or am I on the right track with setting "C = a%b"?

Shawn Vogler
Shawn Vogler
564 Points

I feel like C has to be in the method as a third variable. Here's what I've updated the code to look like:

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


mod (10), (7), (c)

I got stuck at the same exact place a few days ago and even today, when I resumed. I have put this question to the Support.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I suspect that you are overthinking this. Again, the challenge never asks for anything to be printed... only returned. So puts will never be used at all. Also, the string you're returning requires a period/full stop at the end. Not including this will cause the challenge to fail. Do not call the method yourself. Treehouse is going to call the method. Calling the method yourself interferes with what Treehouse is sending.

Here's a solution in one line of code:

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

Here's another solution using a third variable:

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

Hope this helps! :sparkles:

Shawn Vogler
Shawn Vogler
564 Points

Thanks again for your help! This makes much more sense.