mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 10:01:38 +00:00
Add Ruby shorthand block syntax examples
This commit is contained in:
parent
ecca2c3837
commit
c6804101b9
@ -430,6 +430,16 @@ def guests(*array)
|
|||||||
array.each { |guest| puts guest }
|
array.each { |guest| puts guest }
|
||||||
end
|
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
|
# Destructuring
|
||||||
|
|
||||||
# Ruby will automatically destructure arrays on assignment to multiple variables.
|
# Ruby will automatically destructure arrays on assignment to multiple variables.
|
||||||
|
Loading…
Reference in New Issue
Block a user