Array은 integer형 숫자를 색인으로 객체를 저장하고 접근하기 위해서 사용하는 클래스다. Array는 C나 Java의 배열과 마찬가지로 0부터 시작한다. C언어와 달리 음의 정수를 색인으로 할 수 있는데, 이 경우 배열의 끝에서 부터 색인을 시작한다. 예를 들어 -1은 배열의 마지막 원소, -2는 마지막에서 두번째 원소다.
새로운 배열을 반환한다. 첫번째 식은 크기가 0인 배열을 반환한다. 2번째 배열은 객체를 size 크기 만큼 가진 배열을 만든다. 세번째 형식은 size 만큼 괄호에 있는 식을 수행하고 그 결과를 배열에 저장한다.
a = Array.new(2, Hash.new)
a[0]['cat'] = 'feline'
a[1]['cat'] = 'Felix'
# Hash 객체를 원소 2개를 가지는 배열을 만든다.
a = Array.new(2) { Hash.new }
a[0]['cat] = 'feline'
# 1 부터 5까지 제곱한 결과를 가지는 배열을 만든다.
squares = Array.new(5) {|i| i*i}
squares
copy = Array.new(squares)
try_convert(obj) -> array or nil
to_ary메서드를 이용해서 obj를 배열로 변환을 한다. 변환에 성공하면 배열을 반환하고 실패하면 nil을 반환한다. 이 메서드는 주로 매개변수가 배열인지 확인하기 위해서 사용한다.
Array
Public Class Methods
[] (*args)
new
- new (size=0, obj=mil)
- new (array)
- new (size) {|index| block}
새로운 배열을 반환한다. 첫번째 식은 크기가 0인 배열을 반환한다. 2번째 배열은 객체를 size 크기 만큼 가진 배열을 만든다. 세번째 형식은 size 만큼 괄호에 있는 식을 수행하고 그 결과를 배열에 저장한다.a = Array.new(2, Hash.new) a[0]['cat'] = 'feline' a[1]['cat'] = 'Felix' # Hash 객체를 원소 2개를 가지는 배열을 만든다. a = Array.new(2) { Hash.new } a[0]['cat] = 'feline' # 1 부터 5까지 제곱한 결과를 가지는 배열을 만든다. squares = Array.new(5) {|i| i*i} squares copy = Array.new(squares)try_convert(obj) -> array or nil
Array.try_convert([1]) #=> [1] Array.try_convert("1") #=> nil if tmp = Array.try_convert(arg) # arg는 배열이다. elsif tmp = String.try_convert(arg) # arg는 string이다. endPublic Instance Method
ary & other_ary -> new_ary
ary * int -> new_ary
ary * str -> new_string
ary + other_ary -> new_ary
ary - other_ary -> new_ary
ary << obj -> ary
ary <=> other_ary -> -1, 0, +1 or nil
ary == other_ary -> bool
ary[index] -> obj or nil
ary[start, length] -> new_ary or nil
ary[range] -> new_ary or nil
slice(index) -> obj
slice(start, length) -> new_ary or nil
slice(range) -> new_ary or nil
ary[index] = obj -> obj
ary[start, length] = obj or other_ary or nil -> obj or other_ary or nil
assoc(obj) -> new_ary or nil
s1 = [ "colors", "red", "blue", "green" ] s2 = [ "letters", "a", "b", "c" ] s3 = "foo" a = [ s1, s2, s3 ] # [ "letters", "a", "b", "c" ] a.assoc("foo") # nilat(index) -> obj or nil
clear -> any
compact -> new_ary
compact! -> ary or nil
concat(other_ary) -> ary
count -> int
count(obj) -> int
count{ |item| block } -> int
ary = [1, 2, 4, 2] ary.count #=> 4 ary.count(2) #=> 2 ary.count{|x|x%2==0} #=> 3cycle(n=nill) {|obj| block} -> nil
cycle(n=nill) an_enumerator
a = ["a", "b", "c"] a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever. a.cycle(2) {|x| puts x } # print, a, b, c, a, b, c.delete (obj) -> obj or nil
delete (obj) { block } -> obj or nil
#!/usr/bin/ruby a = [ "a", "b", "b", "b", "c" ] a.delete("b") #=> "b" a #=> ["a", "c"] a.delete("z") #=> nil a.delete("z") { "not found" } #=> "not found"delete_at (index) -> obj or nil
delete_if {|item| block} -> ary
delete_if -> en_enumerator
a = [ "a", "b", "c" ] a.delete_if {|x| x >= "b" } #=> ["a"]droip (n) -> new_ary
each {|item| block} -> ary
each -> an_enumerator
a = [ "a", "b", "c" ] a.each {|x| print x, " -- " }each_index {|index| blok} -> ary
a = [ "a", "b", "c" ] a.each_index {|x| print x, " -- " } # 0 -- 1 -- 2 --enpty? -> true or false
eql? (other) -> true or false
fetch (index) -> obj
fetch (index, default) -> obj
fetch (index) {|index| block} -> obj
a = [ 11, 22, 33, 44 ] a.fetch(1) #=> 22 a.fetch(-1) #=> 44 a.fetch(4, 'cat') #=> "cat" a.fetch(4) { |i| i*i } #=> 16fill (obj) -> ary
fill (obj, start [, length]) -> ary
fill (obj, range) -> ary
fill {|index| block} -> ary
fill (start [,length]) {|index| block} -> ary
fill (range {|index| block} -> ary
a = [ "a", "b", "c", "d" ] a.fill("x") #=> ["x", "x", "x", "x"] a.fill("z", 2, 2) #=> ["x", "x", "z", "z"] a.fill("y", 0..1) #=> ["y", "y", "z", "z"] a.fill {|i| i*i} #=> [0, 1, 4, 9] a.fill(-2) {|i| i*i*i} #=> [0, 1, 8, 27]index (obj) -> int or nil
index {|item| block} -> int or nil
index -> an_enumerator
a = [ "a", "b", "c" ] a.index("b") #=> 1 a.index("z") #=> nil a.index{|x|x=="b"} #=> 1first -> obj or nil
first(n) -> new_ary
flatten -> new_ary
flatten (level) -> new_ary
Recent Posts
Archive Posts
Tags