[关闭]
@smilence 2020-01-29T05:47:29.000000Z 字数 2677 阅读 796

Ruby Cheatsheet

入门编程与算法


1. String

  1. num = 0
  2. str_1 = "ab"
  3. str_2 = "xy"
  4. # 1.1 插入, 结合
  5. str_1 + str_2 # => "abxy"
  6. str_1 + num.to_s # => "ab0"
  7. str_2 + 'z' # => "xyz"
  8. str_1 << 'c' # => str_1 became "abc"
  9. # 1.2 读取
  10. str_1[0] # => 'a'
  11. str_1[1..-1] # => 'bc' 取substring window
  12. str_1[0...-1] # => 'ab'
  13. # 1.3 反向
  14. reversed_str = str_1.reverse # reversed_str became "cba", str_1 stay the same
  15. str_1.reverse! # str_1 became "cba"
  16. # 1.4 查找
  17. str_1 = "abc"
  18. str_1.include?('w') # => false, 也可以解读为 w 是不是属于str_1中的一个
  19. str_1.include?("bc") # => true
  20. str_1.index('b') # => 1
  21. str_1.index("bc") # => 1
  22. # 1.5 循环
  23. i = 0
  24. while i < str.length
  25. # do something about str[i]
  26. i += 1
  27. end
  28. str.each_char do |char|
  29. # do something about char
  30. end
  31. str.each_char.with_index do |char, idx|
  32. # do something about char and idx
  33. end

2. Array

  1. arr_ints = [1, 2, 3]
  2. arr_strs = ["I", "am", "April"]
  3. # 2.1 插入, 删除, 结合
  4. arr_ints << 4 # => [1, 2, 3, 4]
  5. arr_strs << "Li" # => ["I", "am", "April", "Li"]
  6. arr_ints + [5, 6] # => [1, 2, 3, 4, 5, 6]
  7. arr_ints.push(7) # => 尾部插入 [1, 2, 3, 4, 5, 6, 7]
  8. ele = arr_ints.pop() # => 尾部取出 arr_ints = [1,2,3,4,5,6], ele = 7
  9. ele = arr_ints.shift() # => 头部往左shift arr_ints = [2,3,4,5,6], ele = 1
  10. arr_ints.unshift(1) # => unshift, 头部插入 [1,2,3,4,5,6]
  11. # 2.2 读取
  12. arr_ints[0] # => 1
  13. arr_ints[1..-1] # => [2, 3, 4, 5, 6]
  14. arr_ints[1...-1] # => [2, 3, 4, 5]
  15. # 2.3 反向
  16. reversed = arr_ints.reverse # reversed became [5,4,3,2,1], arr_ints stay same
  17. arr_ints.reverse! # arr_ints became [5,4,3,2,1]
  18. # 2.4 查找
  19. arr_ints.include?(2) # true
  20. arr_ints.index(2) # => 1
  21. # 2.5 循环
  22. arr = [1, 2, 3, 4]
  23. # while--------
  24. i = 0
  25. while i < arr.length
  26. # do something about arr[i]
  27. i += 1
  28. end
  29. # each --------
  30. arr.each do |ele|
  31. # do something about ele
  32. end
  33. # each.with_index --------
  34. arr.each.with_index do |ele, idx|
  35. # do something about ele and idx
  36. end
  37. # map --------
  38. new_arr = arr.map do |ele|
  39. ele * 2 # specify the new element here
  40. end
  41. # select --------
  42. filtered_arr = arr.select do |ele|
  43. ele > 0 # specify the condition for the new element here
  44. end

3. Loop

  1. # 3.1 number range
  2. # execute from 1 to 3
  3. (1..3).each do |n|
  4. # do something about n
  5. end
  6. # execute from 1, and before 3
  7. (1...3).each do |n|
  8. # do something about n
  9. end
  10. # execute 3 times
  11. 3.times do
  12. # do same thing
  13. end
  14. # 3.2 combinations
  15. arr = ['A', 'K', 'Q']
  16. # 3.2.1 list element pairs
  17. hands = []
  18. arr.each do |ele1|
  19. arr.each do |ele2|
  20. hands << ele1 + ele2
  21. end
  22. end
  23. # hands = ["AA", "AK", "AQ", "KK", "KA", "KQ", "QA", "QK", "QQ"] ~ 3*3 = 9
  24. # 3.2.2 list unique pairs
  25. # assume only one A, one K, one Q
  26. hands = []
  27. arr.each.with_index do |ele1, idx1|
  28. arr.each.with_index do |ele2, idx2|
  29. if idx2 > idx1
  30. hands << ele1 + ele2
  31. end
  32. end
  33. end
  34. # hands = ["AK", "AQ", "KQ"] ~ 3*2/2 = 3

4. Hash

Hash就是一组 {key => value} pair, 可以把array看做key为index 0,1,2,3...的特殊hash

  1. hash = {}
  2. # 4.1 写入
  3. hash["AA"] = 2
  4. hash["KK"] = 3 # hash = { "AA" => 2, "KK" => 3 }
  5. # 4.2 读取
  6. hash["KK"] # => 3
  7. hash["QQ"] # => nil
  8. # 4.3 用于记录profile
  9. profile = {
  10. "name" => "April"
  11. "age" => 18
  12. "height" => 171
  13. }
  14. profile["age"] # => 18
  15. # 4.4 用于计数
  16. counter = Hash.new(0) # 新建一个Hash, 默认value总是0
  17. str = "neswnsws"
  18. str.each_char do |char|
  19. counter[char] += 1
  20. end
  21. counter["n"] == counter["s"] # false
  22. # 4.5 hash的遍历
  23. counter.each do |k, v|
  24. # do something about k and v
  25. end
  26. pair_arr = count.sort_by {|k,v| v} # => [["e", 1], ["n", 2], ["w", 2], ["s", 3]]
  27. pair_arr[-1][0] # => get the max element, "s"
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注