summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMax Schumacher <maximilianbschumacher@gmail.com>2020-07-08 02:02:57 +0200
committerGitHub <noreply@github.com>2020-07-08 02:02:57 +0200
commita54ad9cd1fe4e9601dcab5a0ff073a6d7149bddd (patch)
tree0cc144455c077d21dbec1eed7355562fc1a4ab80
parent93ca3f662a594df087ffdcf9fedc3216ed2d403b (diff)
parent20a468579ac87bd56f1f9e95a7f065e3519522af (diff)
Merge pull request #3921 from claudiosecco/ruby-useful-tricks
[ruby/en] Ruby useful tricks
-rw-r--r--ruby.html.markdown8
1 files changed, 8 insertions, 0 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown
index d21c727f..4b96b71c 100644
--- a/ruby.html.markdown
+++ b/ruby.html.markdown
@@ -181,6 +181,9 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
# Arrays can contain different types of items.
[1, 'hello', false] #=> [1, "hello", false]
+# You might prefer %w instead of quotes
+%w[foo bar baz] #=> ["foo", "bar", "baz"]
+
# Arrays can be indexed.
# From the front...
array[0] #=> 1
@@ -324,6 +327,11 @@ puts doubled
puts array
#=> [1,2,3,4,5]
+# another useful syntax is .map(&:method)
+a = ["FOO", "BAR", "BAZ"]
+a.map { |s| s.downcase } #=> ["foo", "bar", "baz"]
+a.map(&:downcase) #=> ["foo", "bar", "baz"]
+
# Case construct
grade = 'B'