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

Brandan Herron
Brandan Herron
4,825 Points

Ruby methods

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.

Can someone help me with this?

method.rb
def mod(a, b)
  #write your code here
end

1 Answer

Michael Hulet
Michael Hulet
47,912 Points

There are 2 key things you need to know to pass this challenge:

  1. How to do string interpolation
  2. How to use the modulo operator

String interpolation basically puts a value inside a string where you want it. In Ruby, this is done by wrapping the expression you want to be evaluated in #{} where you want it to be in the string. For example:

"5 times 8 is #{5 * 8}" #Evaluates to "5 times 8 is 40"

You can put any equation or method call or raw variable you want in there, and it'll be pasted into the string where it is, without the #{}


The other thing you need to know how to use is the modulo operator, which is just like the division operator, but it gives you the remainder of dividing the 2 numbers. The modulo operator is the percent sign (%). For example, 8 % 4 is 0 (4 goes into 8 twice with no remainder), and 7 % 3 is 1 (3 goes into 7 twice, with a remainder of 1)


Knowing all that, you just have to make your function return a string in the format that the challenge asks for