SlideShare a Scribd company logo
2014年3月29日
Ruby初級者向けレッスン 48回
— Array と Hash —
ひがき @ Ruby関西
Array と Hash
• Array とは
• Hash とは
• Array・Hash オブジェクトの作り方
• 繰り返し
• オブジェクトのコピー
Array とは
• 配列クラス
• 任意のオブジェクトを持つことができる
[1, 1, 2, 3]
[1, "2nd", [3, "3"], 4.0, :five]
Array とは (2)
a = [1, "2nd", [3, "3"], 4.0, :five]
a[0] # => 1
a[1] # => "2nd"
a[1] = :two
a[3, 2] # => [4.0, :five]
a[1..-2] # => [:two, [3, "3"], 4.0]
a[5] # => nil
Array オブジェクトの作り方
["a", "b", "c"] # => ["a", "b", "c"]
("a".."c").to_a # => ["a", "b", "c"]
[*"a".."c"] # => ["a", "b", "c"]
%w[a b c] # => ["a", "b", "c"]
%i[a b c] # => [:a, :b, :c]
Array の初期化
Array.new(3){0} # => [0, 0, 0]
Array.new(3){|i| i.to_s}
# => ["0", "1", "2"]
String から Array へ
"No Ruby, No Life.".scan(/w+/)
# => ["No", "Ruby", "No", "Life"]
"Ruby 関西".scan(/p{Word}+/)
# => ["Ruby", "関西"]
"1,1,2,3,5,8".split(/,/)
# => ["1", "1", "2", "3", "5", "8"]
Array から String へ
["matz", 48, "dhh", 34].join(’,’)
# => "matz,48,dhh,34"
"%s(%d)" % ["matz", 48] # => "matz(48)"
Hash とは
• 連想配列クラス
• 任意のオブジェクトを持つことができる
• 任意のオブジェクトをキーにできる
{0 => "one", "2" => 3, [4, "4"] => :five}
{:AAPL=>566.71, :GOOG=>605.23}
{AAPL: 566.71, GOOG: 605.23}
# => {:AAPL=>566.71, :GOOG=>605.23}
Hash とは (2)
h = {:AAPL=>566.71, :GOOG=>605.23}
h[:AAPL] # => 566.71
h[:MSFT] = 31.16
h[:FB] # => nil
Hash のデフォルト値
sum = Hash.new{|h, k| h[k] = 0}
sum[:FB] # => 0
sum[:TWTR] += 1
sum # => {:FB=>0, :TWTR=>1}
Hash から Array へ
{matz: 48, dhh: 34}.to_a
# => [[:matz, 48], [:dhh, 34]]
[[:matz, 48], [:dhh, 34]].to_h
# => {:matz=>48, :dhh=>34}
Array から Hash へ
a = [:matz, 48, :dhh, 34]
Hash[*a] # => {:matz=>48, :dhh=>34}
繰り返し each
[0, 1, 2].each{|i| puts i}
[0, 1, 2].each do |i|
puts i
end
# >> 0
# >> 1
# >> 2
繰り返し Enumerable
• 繰り返しを行なうクラスのための Mix-in
• クラスには each メソッドが必要
Array.ancestors
# => [Array, Enumerable, Object, Kerne
Hash.ancestors
# => [Hash, Enumerable, Object, Kernel
繰り返し Enumerable (2)
a = [1, 2, 3, 5]
a.map{|i| i * i} # => [1, 4, 9, 25]
a.select{|i| i.odd?} # => [1, 3, 5]
a.inject{|s, i| s + i} # => 11
a.find{|i| i.odd?} # => 1
a.all?{|i| i.even?} # => false
a.any?{|i| i.even?} # => true
inject
a = [1, 2, 3, 5]
a.inject do |s, i|
s # => 1, 3, 6
i # => 2, 3, 5
s + i # => 3, 6, 11
end
Array のコピー
a = [1, 2, 3]
b = a # => [1, 2, 3]
a[0] = 0
a # => [0, 2, 3]
b # => [0, 2, 3]
Array のコピー (2)
a = [1, 2, 3] a 0 1 2
1 2 3
E
  © c dd‚
b = a
a[0] = 0
Array のコピー (2)
a = [1, 2, 3] a 0 1 2
1 2 3
E
  © c dd‚
b = a b  
 
 
a[0] = 0
Array のコピー (2)
a = [1, 2, 3] a 0 1 2
1 2 3
E
c dd‚
b = a b  
 
 
a[0] = 0 0
c
Array のコピー (3)
a = [a, b, c]
b = a.clone # = [a, b, c]
a[0] = A
a # = [A, b, c]
b # = [a, b, c]
Array のコピー (4)
a = [a, b, c]
b = a.clone # = [a, b, c]
a[1].upcase!
a # = [a, B, c]
b # = [a, B, c]
Array のコピー (5)
a = [a, b, c]
b = a.clone
a[0] = A
a[1].upcase!
a 0 1 2
a b c
E
  © c dd‚
Array のコピー (5)
a = [a, b, c]
b = a.clone
a[0] = A
a[1].upcase!
a 0 1 2
a b c
E
  © c dd‚
b 0 1 2E
dds T   
Array のコピー (5)
a = [a, b, c]
b = a.clone
a[0] = A
a[1].upcase!
a 0 1 2
a b c
E
c dd‚
b 0 1 2E
dds T   
A
T
Array のコピー (5)
a = [a, b, c]
b = a.clone
a[0] = A
a[1].upcase!
a 0 1 2
a B c
E
c dd‚
b 0 1 2E
dds T   
A
T
演習問題 0
今日のレッスンで分からなかったこと、疑問に
思ったことをグループで話し合ってみよう。
演習問題 1
map を使わずに map と同じ結果を作ってみよう。
a = [1, 2, 3, 5]
# a.map{|i| i * i} # = [1, 4, 9, 25]
result = []
a.each do |i|
…
演習問題 2
select を使わずに select と同じ結果を作って
みよう。
a = [1, 2, 3, 5]
# a.select{|i| i.odd?} # = [1, 3, 5]
演習問題 3
inject を使わずに inject と同じ結果を作って
みよう。
a = [1, 2, 3, 5]
# a.inject{|s, i| s + i} # = 11
演習問題 4
与えられた文字列から
• 単語の出現回数
• 文字の出現回数
を数えてみよう。
自己紹介
• 名前 (ニックネーム)
• 普段の仕事・研究内容・代表作
• Ruby歴・コンピュータ歴
• 勉強会に来た目的
• などなど
参考
• 公式サイト
https://www.ruby-lang.org/
• るりま
http://docs.ruby-lang.org/ja/
• 解答例
https://github.com/higaki/
learn ruby kansai 60

More Related Content

PPTX
Ruby's Arrays and Hashes with examples
PPTX
Ruby Language: Array, Hash and Iterators
PDF
Useful javascript
PDF
Palestra sobre Collections com Python
PDF
Python fundamentals - basic | WeiYuan
PDF
Clustering com numpy e cython
PDF
第二讲
PDF
轻量级文本工具集
Ruby's Arrays and Hashes with examples
Ruby Language: Array, Hash and Iterators
Useful javascript
Palestra sobre Collections com Python
Python fundamentals - basic | WeiYuan
Clustering com numpy e cython
第二讲
轻量级文本工具集

What's hot (20)

PDF
Groovy collection api
PDF
Some Pry Features
PDF
Queue in swift
PDF
A Taste of Python - Devdays Toronto 2009
PDF
Debugging: A Senior's Skill
PDF
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
PDF
Purely functional data structures
PDF
Text Mining using Regular Expressions
PPT
Csharp4 arrays and_tuples
PDF
1 pythonbasic
PDF
python高级内存管理
PDF
Becoming a better developer with EXPLAIN
PDF
2015 11-17-programming inr.key
PDF
AJUG April 2011 Raw hadoop example
PPTX
Python basic
PDF
Τα Πολύ Βασικά για την Python
PPT
Erlang Concurrency
Groovy collection api
Some Pry Features
Queue in swift
A Taste of Python - Devdays Toronto 2009
Debugging: A Senior's Skill
PLOTCON NYC: Behind Every Great Plot There's a Great Deal of Wrangling
Purely functional data structures
Text Mining using Regular Expressions
Csharp4 arrays and_tuples
1 pythonbasic
python高级内存管理
Becoming a better developer with EXPLAIN
2015 11-17-programming inr.key
AJUG April 2011 Raw hadoop example
Python basic
Τα Πολύ Βασικά για την Python
Erlang Concurrency
Ad

Similar to Ruby初級者向けレッスン 48回 ─── Array と Hash (20)

PDF
4.1 PHP Arrays
KEY
Potential Friend Finder
PDF
Scientific Computing with Python - NumPy | WeiYuan
PDF
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...
PPTX
P3 2018 python_regexes
PPTX
Plc (1)
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
PDF
Python Data Science Cheat Sheet NumPy Basics 3 .pdf
PPT
Ken20150417
PDF
PDF
Numpy python cheat_sheet
PDF
Python_cheatsheet_numpy.pdf
PDF
Numpy python cheat_sheet
PPTX
C-Programming Arrays.pptx
PPTX
C-Programming Arrays.pptx
PDF
PHP and MySQL Tips and tricks, DC 2007
PPTX
Web Application Development using PHP Chapter 4
PDF
Introduction to NumPy
PDF
Introduction to NumPy (PyData SV 2013)
4.1 PHP Arrays
Potential Friend Finder
Scientific Computing with Python - NumPy | WeiYuan
Extending Spark SQL API with Easier to Use Array Types Operations with Marek ...
P3 2018 python_regexes
Plc (1)
NUMPY LIBRARY study materials PPT 2.pptx
Python Data Science Cheat Sheet NumPy Basics 3 .pdf
Ken20150417
Numpy python cheat_sheet
Python_cheatsheet_numpy.pdf
Numpy python cheat_sheet
C-Programming Arrays.pptx
C-Programming Arrays.pptx
PHP and MySQL Tips and tricks, DC 2007
Web Application Development using PHP Chapter 4
Introduction to NumPy
Introduction to NumPy (PyData SV 2013)
Ad

More from higaki (14)

PDF
Ruby初級者向けレッスン 56回 ─── ブロック
PDF
Ruby初級者向けレッスン KOF2015 出張版
PDF
Ruby初級者向けレッスン 55回 ─── 例外
PDF
Ruby初級者向けレッスン 54回 ─── クラス
PDF
Ruby初級者向けレッスン 53回 ─── Array と Hash
PDF
初級者向けレッスン 52回 ─── 文字列
PDF
初級者向けレッスン 51回 ─── 例外
PDF
Ruby初級者向けレッスン 50回 ─── ブロック
PDF
PHPer のための Ruby 教室
PDF
Ruby 初級者向けレッスン 49回───クラス
PDF
Ruby初級者向けレッスン 47回 ─── 文字列
PDF
Ruby初級者向けレッスン 第46回 ─── Test::Unit
PDF
ジュンク堂書店の方から来ました
PDF
Ruby初級者向けレッスン 45回 ─── 例外
Ruby初級者向けレッスン 56回 ─── ブロック
Ruby初級者向けレッスン KOF2015 出張版
Ruby初級者向けレッスン 55回 ─── 例外
Ruby初級者向けレッスン 54回 ─── クラス
Ruby初級者向けレッスン 53回 ─── Array と Hash
初級者向けレッスン 52回 ─── 文字列
初級者向けレッスン 51回 ─── 例外
Ruby初級者向けレッスン 50回 ─── ブロック
PHPer のための Ruby 教室
Ruby 初級者向けレッスン 49回───クラス
Ruby初級者向けレッスン 47回 ─── 文字列
Ruby初級者向けレッスン 第46回 ─── Test::Unit
ジュンク堂書店の方から来ました
Ruby初級者向けレッスン 45回 ─── 例外

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Spectroscopy.pptx food analysis technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
Teaching material agriculture food technology
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
A comparative analysis of optical character recognition models for extracting...
MIND Revenue Release Quarter 2 2025 Press Release
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Programs and apps: productivity, graphics, security and other tools
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Approach and Philosophy of On baking technology
20250228 LYD VKU AI Blended-Learning.pptx
NewMind AI Weekly Chronicles - August'25-Week II
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Unlocking AI with Model Context Protocol (MCP)
Spectroscopy.pptx food analysis technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Teaching material agriculture food technology
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
A comparative analysis of optical character recognition models for extracting...

Ruby初級者向けレッスン 48回 ─── Array と Hash