📄 00 - introduction
字号:
[1, 2, 3, 4].each { |x| puts x }# 1# 2# 3# 4#---[1, 2, 3, 4].collect { |x| x ** 2 } # => [1, 4, 9, 16]#---['a', 'b', 'c'].each_with_index do |item, index| puts "At position #{index}: #{item}"end# At position 0: a# At position 1: b# At position 2: c#---[1, 2, 3, 4].reverse_each { |x| puts x }# 4# 3# 2# 1#---array = ['a', 'b', 'c']array.collect! { |x| x.upcase }array # => ["A", "B", "C"]array.map! { |x| x.downcase }array # => ["a", "b", "c"]#---array = ['junk', 'junk', 'junk', 'val1', 'val2']3.upto(array.length-1) { |i| puts "Value #{array[i]}" }# Value val1# Value val2array = ['1', 'a', '2', 'b', '3', 'c'](0..array.length-1).step(2) do |i| puts "Letter #{array[i]} is #{array[i+1]}"end# Letter 1 is a# Letter 2 is b# Letter 3 is c#---for element in ['a', 'b', 'c'] puts elementend# a# b# cfor element in (1..3) puts elementend# 1# 2# 3#---array = ['cherry', 'strawberry', 'orange']for index in (0...array.length) puts "At position #{index}: #{array[index]}"endindex = 0while index < array.length puts "At position #{index}: #{array[index]}" index += 1 endindex = 0until index == array.length puts "At position #{index}: #{array[index]}" index += 1 end# At position 0: cherry# At position 1: strawberry# At position 2: orange#---array = [1,2,3,4,5]new_array = []front_index = 0back_index = array.length-1while front_index <= back_index new_array << array[front_index] front_index += 1 if front_index <= back_index new_array << array[back_index] back_index -= 1 endendnew_array # => [1, 5, 2, 4, 3]#---class Array def each_from_both_sides front_index = 0 back_index = self.length-1 while front_index <= back_index yield self[front_index] front_index += 1 if front_index <= back_index yield self[back_index] back_index -= 1 end end end endnew_array = [][1,2,3,4,5].each_from_both_sides { |x| new_array << x }new_array # => [1, 5, 2, 4, 3]#---class Array def collect_from_both_sides new_array = [] each_from_both_sides { |x| new_array << yield(x) } return new_array endend["ham", "eggs", "and"].collect_from_both_sides { |x| x.capitalize }# => ["Ham", "And", "Eggs"]#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -