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

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,860 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,888 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,860 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,888 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. :)