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

Why these codes are not correct? def mod(a, b) c = a % b puts "The remainder of a divided by b is #{c}" end

I tested these codes in irb and it works but not in Workspace.

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

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

3 Answers

Hi Andy,

Interpolate a and b too so the values appear in your string. Do the same as you did with c. Note, you can interpolate an expression, so you didn't need to create c, interpolating #{a % b} is fine.

Also, you want to return the string, not puts it.

Make sense?

Steve.

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

Bummer in my console. Is this code correct?

Not quite right there; delete puts and type return instead.

Strictly, you don't need the return keyword at all as Ruby returns the last line evaluated in every method. But sometimes it's just clearer to use the return keyword as it makes it totally obvious what you intend to happen.

So, you could just have:

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

But it is a little clearer like this:

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

Steve.

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

Thank you Steve, I got it.

I love Treehouse!

:+1: :smile: