what is the point of return in Ruby?

前端 未结 7 2151
日久生厌
日久生厌 2020-12-02 18:20

What is the difference between return and just putting a variable such as the following:

no return

def write_code(number_of_errors)
           


        
7条回答
  •  误落风尘
    2020-12-02 18:36

    Using "return" is unnecessary if it is the last line to be executed in the method, since Ruby automatically returns the last evaluated expression.

    You don't even need that final "mood", nor do you need those assignments in the IF statement.

    def write_code(number_of_errors)
        if number_of_errors > 1
           "ERROR"
        else
           "No Problem"
        end  
    end
    
    puts write_code(10)
    

    Output:

    ERROR

提交回复
热议问题