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

Rachel Hutchings
Rachel Hutchings
4,293 Points

How do I define a third variable in Ruby when there are only 2 arguments allowed?

I am supposed to be putting the term "The remainder of a divided by b is c", I am able to define what number a and b is but I am not sure how to define c as being the sum of a divided by b? I keep on thinking I have it figured out and it just gets messier. I'm probably missing something small and obvious.

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

puts mod(4, 2)

2 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

You need to store the result of a%b in c. Right now you are doing the math in the output string. Store the result in c, then output c.

Rachel Hutchings
Rachel Hutchings
4,293 Points

How would you do that? I know it should be "is #{c}" and then return c but I'm not sure how to define c in the code.

Seth Kroger
Seth Kroger
56,413 Points

Defining a new variable is just assigning it in ruby. New variable within methods are only valid within the method (local).

def mod(a, b)
  #write your code here
  c = a%b # creates a variable name c and assigns the value of the remainder to it.
  puts "The remainder of #{a} divided by #{b} is #{c}"
end

puts mod(4, 2)