Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Basics (Retired) Ruby Methods Method Returns: Part 2

Not sure what I'm doing wrong...

I'm not sure what exactly I'm doing wring, but it's probably to do with the end of my string...any suggestions?

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

This is the question: 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.

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,631 Points

Hey Melodie,

You're on the right track, but there are a couple of things:

  1. You'll need to do the calculation for a % b before the string and store it in a variable called c.

  2. The challenge is asking you to return the string, but you are using puts.

  3. There is no purpose of the last return statement.

Below is the correct code for your reference. Have a look, and I hope it'll make sense.

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

:dizzy:

Thank you! Worked like a charm! :)

Tim Knight
Tim Knight
28,863 Points

Melodie,

You want to make sure to actually run the calculation between a and b. Try something like this:

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

One of the benefits of Ruby is that the last line by default will be returned so you don't have to specify a return value.

Jason Anders
Jason Anders
Treehouse Moderator 145,631 Points

Hey Tim.

Guess you type a bit faster. Lol. But, you're missing the period... and we all know how picky the challenges are. :smiley:

Tim Knight
Tim Knight
28,863 Points

You're right Jason, I've made that slight modification to make sure to remove any chance for error. Thanks for that catch.

Thank you both, both work. :)