Ruby Array
2015-07-22 14:00
381 查看
Ruby Array
Initialize
Operate
#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.
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:
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.
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.
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
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
相关文章推荐
- Ruby中的chop和chomp
- 理解 Ruby Symbol (Ruby中的符号)
- [Error]Can't install RMagick 2.13.4. You must have ImageMagick 6.4.9 or later.
- 【编程语言】【Ruby】语言介绍
- Ruby String(2)
- Ruby Programming
- Ubuntu下搭建Ruby on Rails开发环境
- ruby设计模式学习笔记 (一)
- ruby环境的配置
- RubyGems简介和使用
- Ruby黑魔法
- Ruby社区应该去Rails化了
- ruby the diference between gets and gets.chomp()
- ruby里面的毒瘤
- Mac 用RVM安装ruby 2.0.0的时候出现错误
- 替换RubyGems 镜像
- ruby里的坑
- 在Ubuntu 15.04中安装Ruby on Rails
- 针对Ruby的Selenium WebDriver安装指南
- Ruby下WebDriver的相关操作指南