SlideShare a Scribd company logo
1 of 86
Ruby on Arrays
2012/4/25 yoyogi.rb
Rubyの配列について
自己紹介

フリーエンジニア

twitter:@nysalor
blog: http://blog.larus.jp/
好きなメソッドはArrayEnumerable#map
配列?


Arrayクラスで定義

[]でくくる

要素は何でもいい
配列の例


[1, 2, 3, 4, 5]
[:alpha, :bravo, :charlie]
[1, ‘second’, :third, [4.1, 4.2, 4.3], { :fifth => ‘element’}]
配列の作り方
配列の作り方

空の配列
配列の作り方

空の配列


a = []
a = Array.new
配列の作り方
配列の作り方

中身の入った配列
配列の作り方

中身の入った配列


a = [1, 3, 5, 7]
a = Array.new(3, 5) #=> [5, 5, 5]
配列の作り方
配列の作り方

文字列から作成
配列の作り方

文字列から作成


a = %w(tinker tailor soldier spy)
a = “battle ship:light cruiser:destroyer”.split(“:”)
配列の作り方
配列の作り方

to_aメソッド
配列の作り方

to_aメソッド

a = (1..3).to_a
a = (“a”..”z”).to_a
a = {“alpha” => “A”, “bravo” => “B”}.to_a
配列の作り方

to_aメソッド
            範囲演算子

a = (1..3).to_a
a = (“a”..”z”).to_a
a = {“alpha” => “A”, “bravo” => “B”}.to_a
要素の取り出し
要素の取り出し

a = ["alpha", "bravo", "charlie"]
要素の取り出し

a = ["alpha", "bravo", "charlie"]



 a[2] #=> “charlie”
 a[0, 3] #=> “alpha”, “bravo”, “charlie”
要素の取り出し
要素の取り出し

a = ["alpha", "bravo", "charlie"]
要素の取り出し

a = ["alpha", "bravo", "charlie"]


 a.first #=> “alpha”
 a.last #=> “charlie”
 a.shift #=> “alpha”
 a.pop #=> “charlie”
要素の取り出し

a = ["alpha", "bravo", "charlie"]


 a.first #=> “alpha”
 a.last #=> “charlie”
 a.shift #=> “alpha”    要素が削除される

 a.pop #=> “charlie”
要素の取り出し

a = ["alpha", "bravo", "charlie"]


 a.first #=> “alpha”
 a.last #=> “charlie”
 a.shift #=> “alpha”    要素が削除される

 a.pop #=> “charlie”    要素が削除される
要素の追加
要素の追加

a = ["alpha", "bravo", "charlie"]
要素の追加

a = ["alpha", "bravo", "charlie"]


 a << “delta”
 a.push(“delta”)
 a.unshift(“zulu”)
配列の演算
配列の演算

a = ["alpha", "bravo", "charlie"]
b = ["charlie", "delta", "echo"]
配列の演算

a = ["alpha", "bravo", "charlie"]
b = ["charlie", "delta", "echo"]


 a+b
 a-b
 a*3
配列の演算
配列の演算

a = ["alpha", "bravo", "charlie"]
b = ["charlie", "delta", "echo"]
配列の演算

a = ["alpha", "bravo", "charlie"]
b = ["charlie", "delta", "echo"]


 a & b #=> [“charlie”]
 a|b
 a == b #=> false
配列の操作
配列の操作

a = ["alpha", "bravo", "charlie"]
配列の操作

a = ["alpha", "bravo", "charlie"]



 a.reverse
 a.shuffle
配列の操作
配列の操作

a = ["alpha", "bravo", "charlie"]
b = ["delta", "echo", "foxtrot"]
c = [b, a]
配列の操作

a = ["alpha", "bravo", "charlie"]
b = ["delta", "echo", "foxtrot"]
c = [b, a]



 c.flatten
 c.flatten.sort
配列の操作
配列の操作

a = ["golf", nil, nil]
b = ["hotel", "hotel", "india"]
配列の操作

a = ["golf", nil, nil]
b = ["hotel", "hotel", "india"]




 a.compact
 b.uniq
破壊的メソッド
破壊的メソッド

a   =   ["alpha", "bravo", "charlie"]
b   =   ["charlie", "delta", "echo"]
c   =   [b, a]
d   =   c.flatten
破壊的メソッド

a = ["alpha", "bravo", "charlie"]
b = ["charlie", "delta", "echo"]
c = [b, a]
d a.reverse
   = c.flatten

 a.reverse!
 a.shuffle!
 c.flatten!
 d.sort!
破壊的メソッド
破壊的メソッド

e = ["golf", nil, nil]
f = ["hotel", "hotel", "india"]
破壊的メソッド

e = ["golf", nil, nil]
f = ["hotel", "hotel", "india"]




 a.compact!
 b.uniq!
破壊的メソッド
破壊的メソッド

a = ["alpha", "bravo", "charlie"]
破壊的メソッド

a = ["alpha", "bravo", "charlie"]


 a.shift
 a.pop
 a.unshift(“zulu”)
 a.push(“golf”)
 a.clear
破壊的メソッド

a = ["alpha", "bravo", "charlie"]


 a.shift
 a.pop
 a.unshift(“zulu”)   これらは!がなくても破壊的


 a.push(“golf”)
 a.clear
文字列化
文字列化

a = ["alpha", "bravo", "charlie"]
b = ["charlie", "delta", "echo"]
文字列化

a = ["alpha", "bravo", "charlie"]
b = ["charlie", "delta", "echo"]



 a.join #=> “alphabravocharlie”
 a.join(“/”) #=> “alpha/bravo/charlie”
配列の評価
配列の評価

a = ["alpha", "bravo", "charlie"]
配列の評価

a = ["alpha", "bravo", "charlie"]




 a.include?(“bravo”) #=> true
 a.all? #=> true
配列の評価
配列の評価

a = ["alpha", "bravo", "charlie"]
配列の評価

a = ["alpha", "bravo", "charlie"]




 a.size #=> 3
 a.count #=> 3
 a.length #=> 3
配列の評価
配列の評価

g = ["juliet", nil, nil]
h = [nil, nil, nil]
配列の評価

g = ["juliet", nil, nil]
h = [nil, nil, nil]



 g.all? #=> false
 g.any? #=> true
 h.any? #=> false
配列の検索
配列の検索

a = ["alpha", "bravo", "charlie"]
配列の検索

a = ["alpha", "bravo", "charlie"]



 a.find(/l/).first
 a.grep(/a/)
イテレーション
イテレーション
a = ["alpha", "bravo", "charlie"]

a.each do |x|
  p x
end
イテレーション
a = ["alpha", "bravo", "charlie"]

a.each do |x|   ブロック変数
  p x
end
イテレーション
イテレーション

a = ["alpha", "bravo", "charlie"]

a.each_with_index do |x, idx|
  p x
  p idx
end
イテレーション
イテレーション
a = ["alpha", "bravo", "charlie"]

a.inject(0) do |result, x|
  result + x.size
end
イテレーション
a = ["alpha", "bravo", "charlie"]
         初期値
a.inject(0) do |result, x|
  result + x.size
end
イテレーション
イテレーション
a = ["alpha", "bravo", "charlie"]

a.map do |x|
  x.reverse
end
イテレーション
イテレーション
a = ["alpha", "bravo", "charlie"]

a.map! do |x|
  x.reverse
end
イテレーション
a = ["alpha", "bravo", "charlie"]

a.map! do |x|   破壊的メソッド
  x.reverse
end
まとめ


配列は奥が深い

破壊的メソッドに注意

イテレーション
質疑応答
質疑応答

if available?
  Question.all.map(&:answer!)
end
ご清聴ありがとうございました

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Ruby on

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n