Upcoming SlideShare
×

# Ruby on

740 views

Published on

Published in: Education, Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Ruby on

1. 1. Ruby on Arrays2012/4/25 yoyogi.rb
2. 2. Rubyの配列について
4. 4. 配列？Arrayクラスで定義[]でくくる要素は何でもいい
5. 5. 配列の例[1, 2, 3, 4, 5][:alpha, :bravo, :charlie][1, ‘second’, :third, [4.1, 4.2, 4.3], { :ﬁfth => ‘element’}]
6. 6. 配列の作り方
7. 7. 配列の作り方空の配列
8. 8. 配列の作り方空の配列a = []a = Array.new
9. 9. 配列の作り方
10. 10. 配列の作り方中身の入った配列
11. 11. 配列の作り方中身の入った配列a = [1, 3, 5, 7]a = Array.new(3, 5) #=> [5, 5, 5]
12. 12. 配列の作り方
13. 13. 配列の作り方文字列から作成
14. 14. 配列の作り方文字列から作成a = %w(tinker tailor soldier spy)a = “battle ship:light cruiser:destroyer”.split(“:”)
15. 15. 配列の作り方
16. 16. 配列の作り方to_aメソッド
17. 17. 配列の作り方to_aメソッドa = (1..3).to_aa = (“a”..”z”).to_aa = {“alpha” => “A”, “bravo” => “B”}.to_a
18. 18. 配列の作り方to_aメソッド 範囲演算子a = (1..3).to_aa = (“a”..”z”).to_aa = {“alpha” => “A”, “bravo” => “B”}.to_a
19. 19. 要素の取り出し
20. 20. 要素の取り出しa = ["alpha", "bravo", "charlie"]
21. 21. 要素の取り出しa = ["alpha", "bravo", "charlie"] a[2] #=> “charlie” a[0, 3] #=> “alpha”, “bravo”, “charlie”
22. 22. 要素の取り出し
23. 23. 要素の取り出しa = ["alpha", "bravo", "charlie"]
24. 24. 要素の取り出しa = ["alpha", "bravo", "charlie"] a.ﬁrst #=> “alpha” a.last #=> “charlie” a.shift #=> “alpha” a.pop #=> “charlie”
25. 25. 要素の取り出しa = ["alpha", "bravo", "charlie"] a.ﬁrst #=> “alpha” a.last #=> “charlie” a.shift #=> “alpha” 要素が削除される a.pop #=> “charlie”
26. 26. 要素の取り出しa = ["alpha", "bravo", "charlie"] a.ﬁrst #=> “alpha” a.last #=> “charlie” a.shift #=> “alpha” 要素が削除される a.pop #=> “charlie” 要素が削除される
27. 27. 要素の追加
28. 28. 要素の追加a = ["alpha", "bravo", "charlie"]
29. 29. 要素の追加a = ["alpha", "bravo", "charlie"] a << “delta” a.push(“delta”) a.unshift(“zulu”)
30. 30. 配列の演算
31. 31. 配列の演算a = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"]
32. 32. 配列の演算a = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"] a+b a-b a*3
33. 33. 配列の演算
34. 34. 配列の演算a = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"]
35. 35. 配列の演算a = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"] a & b #=> [“charlie”] a|b a == b #=> false
36. 36. 配列の操作
37. 37. 配列の操作a = ["alpha", "bravo", "charlie"]
38. 38. 配列の操作a = ["alpha", "bravo", "charlie"] a.reverse a.shufﬂe
39. 39. 配列の操作
40. 40. 配列の操作a = ["alpha", "bravo", "charlie"]b = ["delta", "echo", "foxtrot"]c = [b, a]
41. 41. 配列の操作a = ["alpha", "bravo", "charlie"]b = ["delta", "echo", "foxtrot"]c = [b, a] c.ﬂatten c.ﬂatten.sort
42. 42. 配列の操作
43. 43. 配列の操作a = ["golf", nil, nil]b = ["hotel", "hotel", "india"]
44. 44. 配列の操作a = ["golf", nil, nil]b = ["hotel", "hotel", "india"] a.compact b.uniq
45. 45. 破壊的メソッド
46. 46. 破壊的メソッドa = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"]c = [b, a]d = c.flatten
47. 47. 破壊的メソッドa = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"]c = [b, a]d a.reverse = c.flatten a.reverse! a.shufﬂe! c.ﬂatten! d.sort!
48. 48. 破壊的メソッド
49. 49. 破壊的メソッドe = ["golf", nil, nil]f = ["hotel", "hotel", "india"]
50. 50. 破壊的メソッドe = ["golf", nil, nil]f = ["hotel", "hotel", "india"] a.compact! b.uniq!
51. 51. 破壊的メソッド
52. 52. 破壊的メソッドa = ["alpha", "bravo", "charlie"]
53. 53. 破壊的メソッドa = ["alpha", "bravo", "charlie"] a.shift a.pop a.unshift(“zulu”) a.push(“golf”) a.clear
54. 54. 破壊的メソッドa = ["alpha", "bravo", "charlie"] a.shift a.pop a.unshift(“zulu”) これらは!がなくても破壊的 a.push(“golf”) a.clear
55. 55. 文字列化
56. 56. 文字列化a = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"]
57. 57. 文字列化a = ["alpha", "bravo", "charlie"]b = ["charlie", "delta", "echo"] a.join #=> “alphabravocharlie” a.join(“/”) #=> “alpha/bravo/charlie”
58. 58. 配列の評価
59. 59. 配列の評価a = ["alpha", "bravo", "charlie"]
60. 60. 配列の評価a = ["alpha", "bravo", "charlie"] a.include?(“bravo”) #=> true a.all? #=> true
61. 61. 配列の評価
62. 62. 配列の評価a = ["alpha", "bravo", "charlie"]
63. 63. 配列の評価a = ["alpha", "bravo", "charlie"] a.size #=> 3 a.count #=> 3 a.length #=> 3
64. 64. 配列の評価
65. 65. 配列の評価g = ["juliet", nil, nil]h = [nil, nil, nil]
66. 66. 配列の評価g = ["juliet", nil, nil]h = [nil, nil, nil] g.all? #=> false g.any? #=> true h.any? #=> false
67. 67. 配列の検索
68. 68. 配列の検索a = ["alpha", "bravo", "charlie"]
69. 69. 配列の検索a = ["alpha", "bravo", "charlie"] a.ﬁnd(/l/).ﬁrst a.grep(/a/)
70. 70. イテレーション
71. 71. イテレーションa = ["alpha", "bravo", "charlie"]a.each do |x| p xend
72. 72. イテレーションa = ["alpha", "bravo", "charlie"]a.each do |x| ブロック変数 p xend
73. 73. イテレーション
74. 74. イテレーションa = ["alpha", "bravo", "charlie"]a.each_with_index do |x, idx| p x p idxend
75. 75. イテレーション
76. 76. イテレーションa = ["alpha", "bravo", "charlie"]a.inject(0) do |result, x| result + x.sizeend
77. 77. イテレーションa = ["alpha", "bravo", "charlie"] 初期値a.inject(0) do |result, x| result + x.sizeend
78. 78. イテレーション
79. 79. イテレーションa = ["alpha", "bravo", "charlie"]a.map do |x| x.reverseend
80. 80. イテレーション
81. 81. イテレーションa = ["alpha", "bravo", "charlie"]a.map! do |x| x.reverseend
82. 82. イテレーションa = ["alpha", "bravo", "charlie"]a.map! do |x| 破壊的メソッド x.reverseend
83. 83. まとめ配列は奥が深い破壊的メソッドに注意イテレーション
84. 84. 質疑応答