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

Michael Lawinger
Michael Lawinger
33,581 Points

Returning Ruby Method

This is the last question for completing the course but I'm stumped on where to go. I tried to follow the same format of the previous video. What am I doing wrong and how do I solve it?

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

puts mod(2,9)

2 Answers

Samuel Ferree
Samuel Ferree
31,722 Points

You're using c in your puts string before assigning it. assign it to the value first.

Per the challenge, don't print the string to the console. Just return it.

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

Using code that's more "ruby-esque", you can just use the arithmetic in the string interpolation, and return without the return statement, like so:

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

Thank you, it's my first time in Ruby and it was a little complicated, but you helped me understand the concept!