Appends the elements of array to arr arr.delete( item)
arr.delete( item) {| item| }
Deletes all elements matching item using == With a block, it returns the result of the block if no elements were deleted arr.delete_at( n) Deletes the nth element of arr arr.delete_if {| x] .} Deletes elements where the value of block is true arr.each {| x| } Invokes the block on each element of arr arr.each_index {| i] }
Invokes the block on each element, passing the index, which is an integer ranging from 0 to arr.length - 1
arr.empty?
Trang 2arr.flatten!
Returns a flattened, one-dimensional array by moving all elements and subelements of arr into the new array [1, [2, 3, [4], 5]].flatten #=> [1, 2, 3, 4, 5] arr.include?( item) arr.member?( item) Returns true if arr contains item as an element arr.index( item)
Returns the index number of the first item in arr equal to item (with 0 being the first index number), or nil if item isn't present arr.indexes([ index ]) arr.indices([ index ]) Returns an array of elements from the specified indexes arr.join([ s=$,]) Returns a string by joining together all elements in arr, separating each substring with s
["foo", "bar].join # => "foobar"
Trang 3arr.map! {|x| .} See arr.collect { |x| } arr.member?( item) See arr.include?(item) arr.nitems Returns the number of elements with non-nil values arr.pack( template)
Trang 6U UTF-8 string V Little-endian short (VAX byte order) V Little-endian long (VAX byte order) Ww BER-compressed integer X Null byte X Backs up one byte Z ASCII string (space padded) @
Moves to absolute position
Each directive may be followed by either a decimal number, indicating the number of elements to convert, or an asterisk, indicating that all remaining elements should be converted Directives may be separated with a space Directives sSillL followed by _ use the native size for that type on the current platform
Trang 7arr.pop Removes the last element from arr and returns it arr.push( obj ) Appends obj to arr arr.rassoc( value)
Trang 9arr.sort {| a, bỊ } arr.sort! {| a, bỊ }
Arrays can be sorted by specifying the conditions for the comparison using a block The block must compare a and b, returning 0 when a == bd, a negative number when a < b, and a positive number when a > D arr.uniq arr.uniq! Deletes duplicate elements from arr arr.unshift( item) Prepends item to arr a = [1,2,3] a.unshift(0) #=> [0,1,2,3| Hash Hash class
Trang 10Creates a Hash A default value may also be specified h= Hash::new(15) #=> {}
h[44| # => 15 (no key; default returned)
Peseta men JJẨsgŠ veễn SSPL TSE SRS EO NS Oe oF RST YS
š tk à các | ES CYS SPER SES
Methods of the Hash class ending in a pipe ! modify their receiver and return a hash if modification took place, otherwise nil Methods without a ! return a modified copy of the hash h[ key] Returns the value associated with key h| key|= value Associates value with key h.clear Deletes all key-value pairs from h h= {1=>2, 2=>4} h.clear h #=> {} h= {1=>2, 2=>4} h.delete_if{|k,v| k % 2 == 0} h # => {1=>2} h.default
Returns the default value for a key that doesn't exist Note that the default value isn't copied, so that modifying the default object may affect all default values thereafter
h.default= value