1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-28 17:11:02 +02:00

Merge pull request #3913 from singjsong/ruby-block-syntax

[ruby/en] Add Ruby shorthand block syntax examples
This commit is contained in:
Max Schumacher
2020-07-07 15:26:33 +02:00
committed by GitHub

View File

@@ -430,6 +430,16 @@ def guests(*array)
array.each { |guest| puts guest }
end
# There is also the shorthand block syntax. It's most useful when you need
# to call a simple method on all array items.
upcased = ['Watch', 'these', 'words', 'get', 'upcased'].map(&:upcase)
puts upcased
#=> ["WATCH", "THESE", "WORDS", "GET", "UPCASED"]
sum = [1, 2, 3, 4, 5].reduce(&:+)
puts sum
#=> 15
# Destructuring
# Ruby will automatically destructure arrays on assignment to multiple variables.