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

string interpolation in methods

how to use string interpolation in methods. i think am doing the correct way..

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

5 Answers

Hi Anoosha,

You just need to include the actual operation within the interpolation, like so:

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

Notice that you need to interpolate a and b as well. So what ends up happening is that you'll call the method with some arguments, like so:

mod(4,2)

and it will return

"The remainder of 4 divided by 2 is 0."

Hope that helps!

Still it is not showing me the correct answer.

What's the error you're getting? What's your code look like?

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. This is my question and the my answer was def mod(a, b) c = a%b return "The remainder of #{a} divided by #{b} is #{c}" end

You need to remove the c = a %b from your code. The challenge is asking you to perform the interpolation inside the returned sentence, not outside of it.

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

Is this is the correct way. If it is correct then why wouldn't it show the answer is correct.

Make sure your return statement has a period at the end..

  return "The remainder of #{a} divided by #{b} is #{a % b}."

I just tried it with a period and without. The code works will the period, and doesn't without it.

Now i got it. Thank you very much. but this is crazy that fate of the challenge was a period.

Yeah, I've been tripped up many times by some little thing like that! Usually when they want something specific printed to the screen in a challenge I just copy paste it into my code, to make sure I'm not missing something. :)

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. This is the error am getting. Bummer! Something doesn't look quite right. Double check your spelling and punctuation.

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