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

Richard Poirier
PLUS
Richard Poirier
Courses Plus Student 2,930 Points

Need Help with code challenge

Finishing last challenge on Ruby basics intro...and for some reason, I m not understanding something. The challenge wants me to make a method called "mod" and do the following:

In the previous challenge, we wrote a method that returned the remainder of two arguments when divided. That's cool, but it would be nicer if the method returned the remainder in a nice full sentence. 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.

What am I missing?

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

2 Answers

Daniel Crews
Daniel Crews
14,008 Points

The question is asking you to 'return' a string from the method. Not puts it. So:

def mod(a, b)
    c = a % b
    return "The remainder of #{a} divided by #{b} is #{c}."
end
Richard Poirier
Richard Poirier
Courses Plus Student 2,930 Points

Thanks Daniel! I could of swore I tried that originally, but maybe I had something typed wrong.

But when it is all said and done...I guess what I am maybe not understanding is the difference between puts and return...I feel like both of them give you an answer, is return used when you want number values and puts is used for just text statements?...I am completely new to programming in general...so that may be a simple (stupid) question...I think I am just a little confused about the difference b/c I didn't pick up on the videos really comparing/contrasting those functions. Again, I really appreciate your help/insight

Daniel Crews
Daniel Crews
14,008 Points

Often times it's something like that period at the end of the string that bites your butt.

Daniel Crews
Daniel Crews
14,008 Points

Think of puts as user interface, if you want to produce human-readable output to a web page, console or file, you puts or print it. In reality, though, the majority of calculation or evaluation results (numbers, strings, booleans...) from method calls on an object are mean't as intermediary steps in program execution and fed into other method call. In this latter case, we use the methods return value or depend upon the actions the method perform. Here's a trivial example:

def add_variables(a, b)

c = a + b

puts c

return true

end

Here, the variables are added, the method puts the value of c to the output, and provides the boolean true as the method's return value to possibly be used elsewhere in the program.

If I were to call the method::

puts add_variables(2, 3)

You would see:

5

true

=> nil

5 is from puts c,

true is from puts add_variables, it puts the return value of the method.

=> nil is the implicit return value of the puts method.

Richard Poirier
Richard Poirier
Courses Plus Student 2,930 Points

Thanks for your great answer!. I appreciate you taking the time...I just got past this part and got stuck on a code challenge a few sections later...lol...The struggle IS real = D