Created
November 11, 2010 17:35
-
-
Save yoggy/672863 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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