Skip to content

Instantly share code, notes, and snippets.

@aeg
Created October 11, 2012 17:28
Show Gist options
  • Save aeg/3874098 to your computer and use it in GitHub Desktop.
Save aeg/3874098 to your computer and use it in GitHub Desktop.
リストの最初と最後の要素を取得する。
// Case #1
// リストの最初の要素を取得する。
def list = ['a', 'b', 'c']
assert list.first() == 'a'
assert list.head() == 'a'
assert list[0] == 'a'
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。
// Case #2
// リストの最後の要素を取得する。
list = ['a', 'b', 'c']
assert list.last() == 'c'
assert list[-1] == 'c'
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。
// Case #3
// リストの最後の要素を取得し、リストから削除する。
list = ['a', 'b', 'c']
assert list.pop() == 'c'
assert list == ['a', 'b']
assert list.pop() == 'b'
assert list == ['a']
// Case #4
// リストの最初の要素を除いたリストを取得する。
list = ['a', 'b', 'c']
assert list.tail() == ['b', 'c']
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。
// Case #5
// リストの最後の要素を除いたリストを取得する。
list = ['a', 'b', 'c']
assert list[0..-2] == ['a', 'b']
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment