Ruby Ruby Objects and Classes Variables and Methods The to_s method

Why dont you need a print statement here

WHy dont you need to write puts to print in a method.

For example we have:

def to_s
    full_name_with_title
end

where is the print statement here we dont use print or puts.

full_name_with_title is defined as :

  def full_name_with_title
    @title + " " + full_name()
  end

there is not print there either

1 Answer

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 47,055 Points

print and puts write a string to the console, which is rarely something you'll want to do, outside of debugging. Instead, in Ruby, the last statement in a method is what that method returns (though you can explicitly write out the return keyword for the same effect, if you want). returning a value from a method makes it so you can use the value that the method generates at other places in your code, including print it out later, if you want