Last active
December 12, 2019 04:17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# yieldを使っても同じことができます | |
# 1から5まで回す処理 | |
def loop_1_to_5(&block) | |
(1..5).each do |v| | |
yield v | |
end | |
end | |
# 1から5までを表示する | |
def display_number | |
loop_1_to_5 do |num| | |
puts num | |
end | |
end | |
# 1から5までを足している | |
def sum_number | |
s_v = 0 | |
loop_1_to_5 do |num| | |
s_v += num | |
end | |
puts s_v | |
end | |
display_number | |
# 1 | |
# 2 | |
# 3 | |
# 4 | |
# 5 | |
sum_number | |
# 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment