Created
July 23, 2012 23:40
-
-
Save kurotaky/3166949 to your computer and use it in GitHub Desktop.
ArrayMethodQuiz
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
# 全部の問題で e = [1,2,3,4,5] を対象に回答してください | |
e = [1,2,3,4,5] | |
e[0] # 1 | |
# 末尾の要素(5)を返すメソッド | |
e[4] # 5 | |
e.slice(4) | |
e.last | |
e.at(4) | |
# 配列のn番目の要素を得るメソッド | |
e.fetch(n) | |
e.at(n) | |
e[n] | |
# 配列が空かどうかを確認するメソッド | |
e.empty? # false 配列が空の時は true | |
# 要素をランダムに一個返すメソッド | |
e.sample | |
# 全ての要素をランダムに入れ替えるメソッド (例 [1,2,3,4,5] => [2,3,1,4,5] にする) | |
e.shuffle # [2, 1, 5, 3, 4] | |
e # [1, 2, 3, 4, 5] | |
e.shuffle! # [5, 4, 3, 1, 2] 破壊的にシャッフル | |
e # [5, 4, 3, 1, 2] | |
# 配列を複製するメソッド | |
e.dup | |
e.clone | |
# 配列を空にするメソッド | |
e.clear | |
e = [] | |
# 末尾に要素(6)を追加する ( [1,2,3,4,5,6] を作る) | |
e + [6] | |
e.push(6) #破壊的に追加 | |
a << 6 | |
# 末尾の要素(5)を削除する ( [1,2,3,4] を作る) | |
e - [5] | |
e.pop | |
# 先頭に要素(0)を追加する ( [0,1,2,3,4,5] を作る) | |
e.unshift(0) | |
# 先頭の要素(1)を削除する ( [2,3,4,5] を作る) | |
e - [1] | |
e.shift(1) | |
# [1,2,3,4,5] を 文字列の "1-2-3-4-5" にする | |
e.join('-') | |
# [1,2,3,4,5] を [1,2,3,"ruby",4,5] にする | |
e = [1,2,3,"ruby",4,5] | |
e.pop(2) | |
e.push "ruby" | |
e.push(4,5) | |
e.insert(3, "ruby") | |
# [1,3,2,5,4] を [1,2,3,4,5] にする | |
e.sort | |
a = [1, 2, 3, 4, 5] | |
e & a | |
e | a | |
# [1,2,3,4,5] を [5,4,3,2,1] にする | |
e.reverse | |
e.reverse! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment