diff options
author | Max Schumacher <maximilianbschumacher@gmail.com> | 2020-07-07 15:26:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-07 15:26:33 +0200 |
commit | b9dfd657c1f12119075971d316a294505e62e9e6 (patch) | |
tree | 458565f13774c6bbdff6cb19c88c82e0557dc7cb /ruby.html.markdown | |
parent | 1d829ae3f95cffd78f9996a5e0e548dd96ffac3d (diff) | |
parent | c6804101b9f60c9083f88b24a1dfa5b91443fc94 (diff) |
Merge pull request #3913 from singjsong/ruby-block-syntax
[ruby/en] Add Ruby shorthand block syntax examples
Diffstat (limited to 'ruby.html.markdown')
-rw-r--r-- | ruby.html.markdown | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown index 376f4a47..d21c727f 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -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. |