summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJake Haber <singjsong@gmail.com>2020-04-21 07:33:51 -0500
committerJake Haber <singjsong@gmail.com>2020-04-21 07:34:57 -0500
commitc6804101b9f60c9083f88b24a1dfa5b91443fc94 (patch)
tree5038576ecb554729794d8cb8e2865727945de60b
parentecca2c38376bb6ac63c148347d6b371ed85bc1b7 (diff)
Add Ruby shorthand block syntax examples
-rw-r--r--ruby.html.markdown10
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.