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

In the previous challenge, we wrote a method that returned the remainder of two arguments when divided. That's cool, but

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.

method.rb
def mod(a, b)
  c = "#{a}" % "#{b}"
  return "#{c}"
end

mod(12,5)
Nadia Masiero
Nadia Masiero
3,468 Points

I'm stuck on this too actually. I'm not entirely sure how to properly add the third variable "c"

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya there! Outside of this challenge, your code might've been on point. But you've to stick to the challenge and instructions given. And your solution is outside the context of the challenge. The solution is pretty straightforward. You just have to return a string using string interpolation in the given function. Here is my code for reference:

def mod(a, b)
  "The remainder of #{a} divided by #{b} is #{a % b}"
end
Nadia Masiero
Nadia Masiero
3,468 Points

Oh! That makes more sense! How does it recognize #{a % b} as being "c"?

Ari Misha
Ari Misha
19,323 Points

Nadia Masiero Think it this way! Lets just say , you stored the value of a % b in c variable and then you used the c variable instead of a % b in the above code. Its gonna render the same thing, isnt it? Also , for the best practices, you dont have to type "return" keyword 'coz Ruby does an implicit return behind the scenes. Like in your code, last line in the function will be returned by Ruby anyway, its a default. (:

~ Ari

Thanks Friend....

Nadia Masiero
Nadia Masiero
3,468 Points

Ari Misha Okay, makes sense! Thank you!! ?