core : array

Array is an ordered list which will resize dynamically. They can be as heterogenous as you would like. Arrays in Brat are zero-based. Arrays also squish in methods from enumerable.
+   <<   ==   array?   clear   compact   compact!   concat   copy   delete_first   deq   each   each_until   each_while   empty?   first   flatten   get   include?   index_of   insert   join   last   length   map   map!   map_with_index   map_with_index!   new   pop   pretty   push   reduce   reject!   rest   reverse   reverse!   reverse_each   reverse_each_while   rindex_of   select!   set   shuffle   shuffle!   sort   sort!   sort_by   sum   to_s   unique   unique!  

Manipulating Arrays

Arrays can be created using the following syntax:

a = [1, 2, 3]

They can also be indexed using the [] syntax:

a = [1, 2, 3]

a[1]  #2
a[0,1]  #[1, 2]
a[1,-1]  #[2, 3]
a[5] = 4  #a is now [1,2,3,null,4]

Note that negative indices are allowed, and that ranges are inclusive.

(array) +

array1 + array2

Create a new array by joining two existing arrays.

(self) «

array « value

Appends value to end of array.

(boolean) ==

array1 == array2

Compares the contents of two arrays.

(boolean) array?

array.array?

Returns true.

(self) clear

array.clear

Empties array.

(array) compact

array.compact

Return a copy of the array with all null values removed.

(array) compact!

array.compact!

Destructively removes all null values for the array.

(self) concat

array.concat array

Appends array to the end of another array.

(array) copy

array.copy

Returns a new array containing all the elements of the original.

(self) delete_first

array.delete_first value

Removes first item in array matching the given value.

(object) deq

array.deq item

Remove item from front of array.

(object) each

array.each block

Invokes the block for each item in the array.

(object) each_until

array.each_until block

Invokes the block for each item in the array, passing in the current index as well.

(object) each_until

array.each_until block

Invokes block for each item. Stops when block returns true.

(object) each_while

array.each_while block

Invokes block for each item. Stops when block returns false or null.

(boolean) empty?

array.empty?

Returns true if the array is empty.

(object) first

array.first

Returns first element in array, or null if the array is empty.

(object) flatten

array.flatten

Flatten all elements into a single array.

(object) get

array.get index
array.get start, end

This may also be called as array[index] or array[start, end]. For a single index, returns the value at the given index. If the index does not have a value or is past the end of the array, null is returned. The index may be negative, in which case -1 is the last element of the array. If a start and an end index are given, this method returns values between the two indexes.

(boolean) include?

array.include? value

Returns true if the array contains the given value.

(number) index_of

array.index_of item
array.index_of item, start

Returns the index of the first item to match the given value. If a start value is given, then the search begins at that index.

(self) insert

array.insert index, value

Inserts the given value into the array at the given position. If the index is beyond the end of the array, the array will be extended to that index. The index may be negative, but the resulting index must already exist.

(string) join

array.join
array.join separator
array.join separator, final

Coverts all elements of the array into strings and joins them together into a single string. If a separator is given, it will be placed in between each element. If a final value is given, it will be inserted in between the last and penultimate values.

 [1,2,3,4].join(", ", ", and ") #=> "1, 2, 3, and 4"

(object) last

array.last

Returns last element in array, or null if the array is empty

(number) length

array.length

Returns the length of the array.

(object) map

array.map block

Invokes the block for each element in the array and returns a new array containing the results.

(object) map!

array.map! block

Invokes the block for each element in the array and replaces that element with the result.

(object) map_with_index

array.map_with_index block

Invokes the block for each element in the array, passing in the index as well, and returns a new array containing the results.

(array) map_with_index!

array.map_with_index! block

Invokes the block for each element in the array, passing in the index as well, and then replaces the element with the result of the block.

(array) new

array.new items

Create a new array.

 array.new 1 2 3 # Returns [1, 2, 3]

(object) pop

array.pop
array.pop items

Removes and returns the last element in the array, or null if the array is empty. If a number of items is specified, removes and returns at most that many items from the end of the array.

(string) pretty

array.pretty

Returns a string with a nicely formatted representation of the array.

(self) push

array.push item

Pushes item onto the end of the array.

(object) reduce

array.reduce block
array.reduce initial, block
array.reduce method_name
array.reduce initial, method_name

Combines elements in array. There are several forms of reduce: one that provides an initial value for memo, one that does not, and two that just provide a method name instead of a function.

 #These are all equivalent:
 1.to(10).reduce 0 { sum, item | sum + item }
 1.to(10).reduce { sum, item | sum + item }
 1.to(10).reduce 0 :+
 1.to(10).reduce :+

(array) reject!

array.reject! block

The first form calls method on each element of the array and removes any elements where that method returns true. The second form removes any element for which the function returns true.

(array) rest

array.rest

Returns the entire array except the first element.

(array) reverse

array.reverse

Returns a copy of the array, reversed.

(self) reverse!

array.reverse!

Reverses the array.

(object) reverse_each

array.reverse_each block

Invokes block for each item in the array, but starts at the end.

(object) reverse_each_while

array.reverse_each_while block

Invokes block for each item in the array, starting from the end. Halts if the block does not return true.

(object) rindex_of

array.rindex_of item
array.rindex_of item, start_index

Returns the last index of the item found in the array, or null if there is no such item. If start_index is specified, start searching from the given index.

(array) select!

array.select! block

The first form takes the name of a method that will be called on elements of the array. All elements whose methods return false will be removed from the array. The second form removes all items for which block returned false.

(value) set

array.set index, value

Set an index to the given value. More commonly called like array[index] = value. If the index is positive and past the end of the array, the array is expanded to the required length. The index may be negative, in which case the indexes begin at the end of the array. However, the resulting index must exist (the array does not expand to accommodate negative indexes).

(array) shuffle

array.shuffle

Returns a copy of the array with the elements shuffled.

(array) shuffle!

array.shuffle!

Shuffles the elements of the array in place.

(array) sort

array.sort

Returns a new array with the contents sorted. All items in the array must be comparable and nonnull.

(self) sort!

array.sort!

Sorts the array in place.

(array) sort_by

array.sort_by block

Returns a new array with the contents sorted using the given function. The function should take two arguments and return true when a < b and false otherwise.

(object) sum

array.sum

Returns the sum of the items in the array. Only works if the array only contains numbers.

(string) to_s

array.to_s

Convert array and contents to strings.

(array) unique

array.unique

Returns a new array containing no duplicate items.

(self) unique!

array.unique!

Removes duplicate items from array.

Fork me on GitHub