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

Alistair Mackay
Alistair Mackay
7,812 Points

Alternative way returning the value?

This method seems to return the correct answer but It's not accepted by the exercise.

Can anyone demonstrate what exactly I am missing from the below code that would allow me to complete the exercise?

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

mod(5, 2)

4 Answers

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

Two things here. And one is a tiny boo-boo! First, you may return the string or puts the string... not both. Secondly, the challenge requires a period at the end of the string or it won't pass. Take a look at my return line (Note: this is your code after I remove the puts and add a period)

  return "The remainder of #{a} divided by #{b} is #{a % b}."
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Sorry, I don't know why it's not putting the quotes in the markdown. This has happened to me twice in the past two days.

Jacob Bergdahl
Jacob Bergdahl
29,118 Points

Puts is a method for printing text -- it cannot be used together with return. Again, to clarify, because I know this can be confusing if you're just starting out: return simply returns the value, puts prints it. Furthermore, the return keyword is actually optional in Ruby, although in complex methods it is preferred for clarity (and of course, if you have multiple points of returns, it is necessary).

So two problems, first, you must make a c variable to store a modulo b.

def mod(a, b)
  c = a % b
end

Next, there is no need to use the puts method, like what Jennifer Nordell and *Jacob Bergdahl * said. So the final step is

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

Don't forget the period at the end of the string!

Oh yeah and one last problem: Don't call the function. The quiz will run it and test it for you. That means don't write this:

mod(5, 2)

Hope it helps! ~xela888

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

It's interesting to note, xela888 that according to the instructions you need the variable c to hold the value. The challenge passes without it :) Mostly challenges are very picky, and would require this. For whatever reason, this particular challenge lets you skip that part.

Alistair Mackay
Alistair Mackay
7,812 Points

Thank you all for your help on this one. It all helped to show me where I was going wrong and more importantly, to help me understand it a little better.