Ruby Array

来源:互联网 发布:网络流行英语词汇 编辑:程序博客网 时间:2024/05/02 18:53
Ruby Array

Initialize

a = Array.newa = []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] = obja[start_index, length] = obj or an_arraya[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 andthe 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”)    #nilff.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
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 欧卡2车卡住了怎么办 欧卡2车子卡住了怎么办 做题手感变差怎么办 吃鸡游戏掉帧怎么办 买了俄区游戏怎么办 战地1子弹用完了怎么办 1kb彩信点开了怎么办 玩地球末日卡顿怎么办 电脑显示不亮了怎么办 电脑玩游戏显卡停止运行怎么办 ie浏览器打开就停止工作怎么办 蝙蝠侠阿甘骑士卡顿怎么办 捷豹钥匙没电了怎么办 车钥匙没电 汽车报警怎么办 东西卡在门锁里怎么办 锁芯里面有东西怎么办 有东西卡在锁里怎么办 锁里面卡了牙签怎么办 塑料卡在锁里怎么办 门锁被牙签堵了怎么办 锁子里面卡东西怎么办 东西卡在锁里怎么办 开车门碰到旁边车门怎么办 美团退款后 物品怎么办 倒车影像是反的怎么办 荒野行动cp版玩着玩着关机怎么办 王者荣耀点击开始游戏就闪退怎么办 电脑遥控游玩ps4画面抖动怎么办 ps4特典不能用了怎么办 苹果x出现的分屏怎么办 电脑注册表文件丢失或损坏怎么办 cad绘图反应很慢怎么办 拍到货商家下架怎么办 电动车头太活了怎么办 快捷方式在根目录找不到了怎么办 神秘海域4卡bug了怎么办 地下城老是闪退怎么办 强制关机后电脑打不开了怎么办 文明5地中海的海军怎么办 文明5被贸易禁运怎么办 文明5海里的食物怎么办