您的位置:首页 > 编程语言 > Ruby

Ruby Array

2015-07-22 14:00 381 查看
Ruby Array

Initialize

a = Array.new
a = []
a = Array.new(n)   #create n elements each value is nil.
a = Array.new(n, m)  #create n elements each value is m.


Operate
a + b     #return a new array with new concatenating with two array, maybe duplicate.
a | b      #return a new array with new join with two array, no duplicate.
a * n      #repeat array a n times.
a - b      # return a new array whose element in a but not in b.
a & b     # return the both in two array, no duplicate.
a <=> b    #compare a with b, return 1 (if great than), 0 (equal with), -1 (less than), ignore order of array.
a == b    #compare a with b, if every response element is equal, return true, else false.

a << val  # push value val to a.

a[index]          a.slice(index)
a[start_index, length]        a.slice(start_index, length)
a[range] such as a[3..6]          a.slice(range)

a[index] = obj
a[start_index, length] = obj or an_array
a[range] = obj or an_array


#set the value at index. or replace the sub-array starting from start_index and continue for length, or replace the sub-array specified by range. Negative indice will count backward the array from end of the array.
Insert array behind the index if length id zero.

Methods

1. abbrev
require ‘abbrev'
array.abbrev(pattern) #return the unambiguous abbreviation for the strings in array. If passed a pattern, only the unambiguous abbreviation match the pattern return.
#eg:
ff = %w(seika seigh tis a)
#=> {"seika"=>"seika", "seik"=>"seika", "seigh"=>"seigh", "seig"=>"seigh", "tis"=>"tis", "ti"=>"tis", "t"=>"tis", "a"=>"a"}


But, what is the use case? the label for bar graph?

2. assoc
build in method. #search the array, if the element (such arr_element )is also array and
the first value of the arr_element equal to the obj, return the first matched element.
eg:
ff = ["seika", "seigh", "tis", ["b", "a"]]
ff.assoc("b”)    # ["b", "a"]
ff.assoc(“a”)    #nil
ff.assoc(“tis”)  # nil


3.rassoc
array.rassoc(obj)
# search the array, if the element (such arr_element )is also array and
the second value
of the arr_element equal to the obj, return the first matched element.
#eg.
2.0.0-p598 :166 > ff
=> [1, 4, 2, 3, 6, 5, 9, 10, [1, 2, 3], [2, 3, 4]]
2.0.0-p598 :167 > ff.assoc(2)
=> [2, 3, 4]
2.0.0-p598 :168 > ff.rassoc(2)
=> [1, 2, 3]


3. map / collect

array.map block array.map! block
array.collect block array.collect! block
# invoke the block for each element of array, and return the new array including the return result of block. If use !, replace the array value with the block return value.

eg:
array.map {|x| x*2}

4. compact
array.compact or array.compact!
return array not include nil

5. delete
array.delete(obj) or array.delete(obj) {block}
delete the element from self which equal to obj, and return obj. If no this value matched, return nil. If block given, when no value matched, return block return value.

6. delete_at
array.delete_at(index)
delete the element itself responce to index

7. delete_if
array.delete_if {|item| block }
delete the element itself when the block is true.

8. each
array.each {|item| block}

recerse_each
array.reverse_each {|item| block }
#same as each, but runs at reverse order.

9. each_index
array.each_index {|index| block}

array.each_with_index {|val, index| block}

10. eql
array.eql? other
if array and other indicate to the same object, or have the same content including order, return true. Else false.

11. fetch
array.fetch(index) #return the value respond to index. If out of range, raise Exception.
array.fetch(index, default) # If out of range, return default.
array.fetch(index) {|index| block} # If out of range, return block return value.

12. fill
array.fill(obj) #replace all the value with obj
array.fill(obj, start [, length]) #replace range value with obj. If start is nil, then (equal to 0), and length is the whole array length if length nil.
array.fill(obj, range)
array.fill { |index| block } replace each value with each the block given.
array.fill(start [, length] ) { |index| block }
array.fill(range) { |index| block }

13. first
array.first
array.first(n)
#return the first value of first n value of array. We can see some special case in which the Array is [].
vv=Array.new
vv.first
vv.first(2)

14. flatten
array.flatten
#Returns a new array that is a one-dimensional flattening of this array (recursively).
array.flatten! #modify the array.
#eg:
2.0.0-p598 :134 > vv = [1,2, [3,4], [[[5,6],[7,8], 9]], 10 , 11]
=> [1, 2, [3, 4], [[[5, 6], [7, 8], 9]], 10, 11]
2.0.0-p598 :135 > vv.flatten
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
2.0.0-p598 :136 > vv.flatten 1
=> [1, 2, 3, 4, [[5, 6], [7, 8], 9], 10, 11]
2.0.0-p598 :137 > vv.flatten 2
=> [1, 2, 3, 4, [5, 6], [7, 8], 9, 10, 11]


Notice: you can specify the recursive deep.

array.pop #return the last value of array. return nil if array is empty
array.push(obj1 [, obj2, obj3]) push the values to the end of array.

reject
array.reject { |item| block } #return a new array containning the element which make the block return false.
array.reject! { |item| block } #delete the element from the array, which make the block true.

reverse
array.reverse #return a new array whose order is reverse.
array.reverse! #reverse the order of array

slice
array.slice # return the value in the range
same as:

a[index] a.slice(index)
a[start_index, length] a.slice(start_index, length)

a[range] such as a[3..6] a.slice(range)

delete the value in the range.
array.slice!(index)
array.slice!(start, length)
array.slice!(range)

sort
array.sort [or] array.sort { | a,b | block }
array.sort! [or] array.sort! { | a,b | block }

uniq
array.uniq
array.uniq!

array.hash #return the hash code of array. the same content array will return same code
array.index(obj) return the index which value is equal to obj
array.last or array.last(n) return the last value ot last n value of array
array.include? obj # return true if obj in array. else false
array.inspect # return the printable array
array.insert(index, obj) # insert the obj to the head of index value in array. the index can be negative.
array.length
array.to_s #return the same value with inspect
array.shift #remove the first value and return
array.unshift(obj, …) #Prepends objects to the front of array, other elements up one.
array.value_at(range) #return the value respond to range
array.value_at(index)

array.value_at(start_index, length)
array.min
array.max
array.minmax #return the array contain min and max value
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: