Created
April 22, 2019 09:53
-
-
Save missingno15/6fea66caa019f5fc9cb97a7957203201 to your computer and use it in GitHub Desktop.
reduceのいろんな使い方
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
# require "pry" | |
puts [1, 2, 3].inspect | |
result_1 = [1, 2, 3].reduce do |sum, n| | |
sum + n | |
end | |
puts result_1.inspect | |
puts | |
puts [14, 15, 16].inspect | |
result_2 = [14, 15, 16].reduce(100) do |acc, n| | |
acc += (n + 10) | |
acc | |
end | |
params = "jalan:1,rakuten:2,travelbook:3".split(",") | |
result_3 = params.reduce([]) do |acc, param| | |
spot = param.split(":") | |
# spot[0], spot[1] | |
provider, key = param.split(":") | |
# binding.pry | |
acc << param | |
end | |
puts result.inspect | |
result_4 = ["mary", "john", "joseph"].reduce(" ") do |acc, str| | |
# binding.pry | |
acc += (" " + str) | |
# binding.pry | |
acc | |
end | |
puts result_4.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment