1

Beginner on Ruby here...below are two similar codes, I understand the procedures of this very basic code but I'd like to understand the theory behind it...

First

 def stats(ppg)
   if ppg > 20
     puts "The PG is considered elite"
   else
     puts "The PG is not considered elite"
   end
 end

 stats(28)

Second

 def stats(ppg)
   if ppg > 20
     "The PG is considered elite"
   else
     "The PG is not considered elite"
   end
 end

 puts stats(28)

So the first piece automatically writes out the strings whereas the second piece does not - is stats(ppg) from the first piece considered an object or a method with the variable as the argument?

3
  • It seems to work fine for me. Maybe I'm missing something? Commented Dec 1, 2014 at 1:01
  • 1
    nothing wrong with the code, just trying to understand the subtle nuances of both ways Commented Dec 1, 2014 at 1:28
  • Sorry, I read the question wrong. Glad you got your answer though. Commented Dec 1, 2014 at 2:25

1 Answer 1

4

def stats(ppg) in both cases is the same thing, i.e. the beginning of a method definition for a method named stats that takes one argument or parameter that will be named ppg inside the method body.

stats(28) in both cases is the same thing, i.e. a call to a method named stats with the literal Fixnum 28 passed as its argument, whereupon it will be assigned to the variable ppg inside the method body from the definition.

In Ruby, every expression involves two distinct phenomena: side effects, and return values. The expression a = 1 has a side effect of assigning the value of 1 to the variable a, and a return value of 1. Not every method in Ruby has a side effect, but every method does have a return value — either an explicit return value (by using return), or else the return value of the last expression evaluated in the method body.

puts is an unfortunate method in Ruby, because it is used in so many beginner examples, yet its behavior is confusing. It has a side effect of printing its argument to stdout, but its return value is nil (which often confuses beginners, who expect it to return the value of its argument).

The difference between your first method and second method is that the first method, because it uses puts internally, has the side effect of printing a string to stdout and a return value of nil, while the second method has no side effect, but a return value of the string itself.

Therefore, when you call your first method without puts in front, the side effect of printing the string occurs and you see the output. When you call your second method, there is no printing side effect, so in order to have the string printed to stdout, you have to call puts. The argument to puts is the return value of your stats method, i.e. the string you wanted to print.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the constructive answer... and since you mentioned puts - I can't reference the literature but came across in a book a longer way to write out the method (so as to illustrate R's programming concept and the operative behavior) - would you happen to know of the alternative method to writing puts? Or a reference?
puts is useful in many cases. It is the best way to write an entire line to a stream, such as stdout ($stdout.puts(my_string)), stderr ($stderr.puts(my_string)), or an open file handler (File.open('somefile.txt','w') {|f| f.puts('my_string') }). The crucial thing to remember is that it returns nil. A common mistake when trying to print a value for debugging purposes is to add a puts in front of the final line of a method body (i.e. the implicit return value), forgetting that this changes the return value of the method to nil as well. Try using p, which returns the value itself.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.