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

Andras Andras
Andras Andras
8,230 Points

Embed return value method in string

Hello,

Sorry I got stuck with methods. Could you please check the code I put.

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

1 Answer

Samuel Ferree
Samuel Ferree
31,722 Points

You're using the value of c before declaring it.

Per the challenge, you don't need to return c, just the string that contains the formatted sentence.

The return statement should also come before c, and not be part of the assignment. See the working code below

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

But to use a more ruby-esque style, we can just do the arithmetic in the string interpolation, and omit the return statement. Ruby will by default return what ever the last statement of a function evaluates to.

def mod (a, b)
   "the remainder of #{a} divided by #{b} is #{a % b}."
end
Andras Andras
Andras Andras
8,230 Points

Thanks Samuel!

I used the second one however without the spaces between characters, probably the translator was not perfect, I'll check it again.