mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Merge pull request #3921 from claudiosecco/ruby-useful-tricks
[ruby/en] Ruby useful tricks
This commit is contained in:
commit
a54ad9cd1f
@ -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'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user