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

Oliver Stamp
Oliver Stamp
3,464 Points

Not quite sure whereI am going wrong, please help.

Method Returns on Ruby basics course, I cannot for the life of me work out where I am going wrong!

Question is:

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.


and i get met with

"" Bummer! NameError: undefined local variable or method `a' for main:Object""

Please help!

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

end
mod (a%b)

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Oliver,

You're more or less on the right track, but there a couple of issues.

  • First, you are using puts when the challenge specifically asks you to return the string.

  • Second, You are interpolating the variable c, but you haven't declared it anywhere. This code would work and pass the challenge if you changed return a % b to c = a % b. (And the puts to a return).

But an even DRYer approach would be to do the actual calculation right in the interpolation.

Below is the code I would use. Have a look, and I hope it makes sense.

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

Keep Coding! :) :dizzy:

Oliver Stamp
Oliver Stamp
3,464 Points

Thank you Jason. That makes total sense now, really appreciate the response!