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

Hi, I'm not sure and I don't know why I can't pass this challenge. I've got right answer showing on preview mode...

So someone know what I doing wrong here or what I didn't understand well... ?

Help. :)

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

mod(4,2)

3 Answers

You have a couple of issues.

It's looking for the remainder so you should use % instead of /

Also, the example output has a period at the end of the string which you're missing. The challenge is looking for an exact match on the output.

Yes I missed full stop there. That's working! Many thanks!

Alex Rainert
Alex Rainert
1,040 Points

This answer was helpful for me. Would either of you be able to quickly explain with its "return" then the string rather than "puts"?

Thanks so much!

a

The reason return is used here is because the challenge is specifically requesting that you do that.

"... Use string interpolation to return the remainder inside the sentence. ..."

If it wanted you to use puts then it would have been something different like "print the string to the console" Then you would know to use puts instead of returning it.

It's not that return is right and puts is wrong but it's more about the requirements of the program or in this case the challenge.

In general, it's more useful to return a value from a function than it is to print it out. So you'll be doing return most of the time instead of puts. If you print it out, then that's it. It's lost and you can't do anything else with it.

If you return the value to the code that called the function, then you have more options. You can print that return value, or save it in a variable for further processing later or use it in an expression, etc...

Alex Rainert
Alex Rainert
1,040 Points

Thanks so much for the thoughtful reply, Jason.

Terrence Beckham
Terrence Beckham
Courses Plus Student 879 Points

Great reply Jason. You cleared up a lot of confusion for me :)