Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created November 11, 2010 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoggy/672863 to your computer and use it in GitHub Desktop.
Save yoggy/672863 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'pp'
a = [
[1,2,3],
[4,5,6],
[7,8,9]
]
b = {
1 => [2, 3],
4 => [5, 6],
7 => [8, 9]
}
# ハッシュっぽく使えるメソッドをArrayクラスに追加してみるテスト
class Array
def has_value?(v)
is_find = false
self.each {|c|
is_find = true if c[0] == v
}
is_find
end
end
# 配列だけのばあい
(1..9).each{|i|
unless a.has_value?(i)
a << [i, 0, 0]
end
}
# ハッシュを使う場合
(1..9).each{|i|
unless b.key?(i)
b[i] = [0, 0]
end
}
pp a
pp b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment