In Ruby, the def
keyword delimits the start of a method, in your case named SimpleAdding
. It takes one argument (in the parentheses) named num
.
In the method body, the variable sum
is given the initial value of 0
.
The line containing (num + 1).times do |x|
tells Ruby to execute the code between the do
and end
keywords a set number of times (an iterator), in this case num + 1
. Remember, num
represents the value received in the form of an argument when the method was called.
On the next line, the variable sum
(initialized to 0
at the beginning of the method) is assigned the value of itself plus x
.
Next line, our iterator end
s.
Finally, we return
the value stored inside of the variable sum
.
And, the end
of our method.
Enjoy learning Ruby!