Merge pull request #3921 from claudiosecco/ruby-useful-tricks

[ruby/en] Ruby useful tricks
This commit is contained in:
Max Schumacher 2020-07-08 02:02:57 +02:00 committed by GitHub
commit a54ad9cd1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -181,6 +181,9 @@ array = [1, 2, 3, 4, 5] #=> [1, 2, 3, 4, 5]
# Arrays can contain different types of items. # Arrays can contain different types of items.
[1, 'hello', false] #=> [1, "hello", false] [1, 'hello', false] #=> [1, "hello", false]
# You might prefer %w instead of quotes
%w[foo bar baz] #=> ["foo", "bar", "baz"]
# Arrays can be indexed. # Arrays can be indexed.
# From the front... # From the front...
array[0] #=> 1 array[0] #=> 1
@ -324,6 +327,11 @@ puts doubled
puts array puts array
#=> [1,2,3,4,5] #=> [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 # Case construct
grade = 'B' grade = 'B'