Ruby on
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Ruby on

on

  • 542 views

 

Statistics

Views

Total Views
542
Views on SlideShare
536
Embed Views
6

Actions

Likes
1
Downloads
7
Comments
0

2 Embeds 6

http://www.hanrss.com 3
http://coderwall.com 3

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Ruby on Presentation Transcript

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