I knew it in my head, but can I explain it again? The real intention is that it is a little difficult to say.
I would like to re-recognize it while thinking in words to deepen my understanding.
Simply put, a mass of processing? It's like that. `def, method name (please decide.) Line break Write a process, start a new line Write end. ``
method.rb
def method name
Write the process here
end
What do you use it for?
For example, in the case of do not use methods to process calculations
sample.rb
eraser = 110
pen = 150
puts eraser + pen
And with use the method
...
sample.rb
def addition
eraser = 110
pen = 150
eraser + pen
end
puts addition
It will be.
If it's simple, you won't feel the meaning or benefit of using the method. But when the process gets complicated, you can feel the tremendous benefits! !!
Various processes such as if can be described in the method. For example ...
sample.rb
def register
eraser = 110
pen = 150
bill = eraser + pen
if bill >= 200 #10 for purchases over 200 yen%off!!
bill * 0.9
end
end
puts register #Call the register method.
And, like this, it will calculate and discount with one method (lump).
That's all for today's output. I think it will continue until the second and third.
Recommended Posts