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 Blocks Blocks Practice Build a Monster Class: Part 4

Nick Vitsinsky
Nick Vitsinsky
7,246 Points

Whats the difference between monster.print_score and puts monster.print_score?

I remember we sometimes called

puts monster.print_score

and sometimes

monster.print_score

So I wonder what is the difference and when to use one form over another?

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

This has to do with how the print_scoreboard method was defined within the Monster class.

  def print_scoreboard
    puts "------------------------------"
    puts "#{name} scoreboard"
    puts "------------------------------"
    puts "- Screams: #{actions[:screams]}"
    puts "- Scares: #{actions[:scares]}"
    puts "- Runs: #{actions[:runs]}"
    puts "- Hides: #{actions[:hides]}"
    puts "------------------------------"
  end

You see, there're multiple lines of puts statements in the print_scoreboard method body, the job of the puts statement is to output the result to your screen; thus by simply calling the monster.print_scoreboard, the result will be outputted to the screen.

If, however, the print_scoreboard was defined in a different fashion, say, sth like the following.

def print_scoreboard
  return "some value"
end

It uses return statement instead of the puts, in this case, by calling monster.print_scoreboard, nothing will be printed, as return statement does not output the result on screen; now it's necessary to pass the method call to puts as argument -- puts monster.print_scoreboard. Does that make sense?

Nick Vitsinsky
Nick Vitsinsky
7,246 Points

So if there're no any puts is in print_scoreboard we must call puts monster.print_scoreboard instead of simply monster.print_scoreboard in order to have something as an output on the screen (I assume that return "some value" should be printed on the screen in this case, what if some value will have puts in it? would it be printed out without puts in monster.print_scoreboard?)

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

I assume that return "some value" should be printed on the screen

No, keep in mind that return statement never prints anything on screen, your program would still calculate the return value behind the scene, just that you won't be seeing anything outputted unless you pass its return result as argument to puts statement.

would it be printed out without puts in monster.print_scoreboard?

If you replace the returns with puts in that sample code I wrote, then yes, calling monster.print_scoreboard is suffice to print the result out.

Try experiment with them on the irb for better understanding :)

I wonder why the teachers never really explain "return" a bit...it seems there is pretty much a common missunderstanding about it in ther forum. I see like 5 posts every day because people think return prints something

Nick Vitsinsky
Nick Vitsinsky
7,246 Points

I see, it's clear now for me.

Thanks, still didn't get used to irb for some reason :(

Nick Vitsinsky
Nick Vitsinsky
7,246 Points

Yeah, Tobias, I agree that actually "return" is confusing sometimes. As we are expecting to see something returned, while it is not.