Skip to content

Instantly share code, notes, and snippets.

@andreaseriksson
Last active December 17, 2015 13:10
Show Gist options
  • Save andreaseriksson/5615035 to your computer and use it in GitHub Desktop.
Save andreaseriksson/5615035 to your computer and use it in GitHub Desktop.
RUBY Array functions
empty? #True if the array is empty
first #The first value in the array
include? #True if the array includes the given value
index #The index number of a given value
inspect #The array in a printable format
last #The last value in the array
length #The number of items in the array
reverse #The array in reverse order
sort #The array sorted
uniq #The unique values in the array
n = ["a", "b", "c", "d", "e"]
n.slice(0) # "a"
n.slice(2, 2) # ["c", "d"]
n[0] # "a"
n[2, 2] # ["c", "d"]
n.last # "e"
n.last(2) # ["d, e"]
n.first(3) # ["a", "b", "c"]
#Find the location of specific values in each array
numbers.index(0.4) #=> 0 (Den första)
numbers.rindex(0.4) #=> 3 (Den sista)
dwarfs.index('Papa Smurf') #=> nil
#Re-order and display the arrays
numbers.sort #Sort numbers lowest to highest
dwarfs.sort.reverse #Sort words in reverse order
#Cut some values from an array
dwarfs.length
oops = dwarfs.slice!(2, 2)
dwarfs.length
#Return only the unique values in an array
numbers.length #=> 5
numbers.uniq.length #=> 4
#The size method is an alias for length
ruby -w scriptname.rb 'Hello, World!' 2 'cat'
ARGV[0] # "Hello, World!"
ARGV[1] # 2
ARGV[2] # "cat"
Array.new(3) #=> [nil, nil, nil]
Array.new(3, true) #=> [true, true, true]
a = [] # a = Array.new
a << 32 # = a.push 'carrot'
#unshift adds elements to the beginning of the array
a = [2, 3, 4]
a.unshift(1) # [1, 2, 3, 4]
#The insert method can be used to add an element anywhere
a.insert(2, 2.5) # [1, 2, 2.5, 3, 4]
#To remove the last element from the array, use pop
a = [1, 2]
a.pop # a = [1]
#To remove an item from the beginning of the array, use shift
a = [1, 2, 3, 4]
a.shift # a = [2, 3, 4]
#To remove an item from the middle of the array
a = [1, 2, 3, 4, 5, 6]
a.delete_at(2) # a = [1, 2, 4, 5, 6]
#To remove an item from the array based upon its value, use delete
pets = ['cat', 'dog', 'rabbit', 'parrot']
a.delete('dog') # pets = ['cat', 'rabbit', 'parrot']
#Multiple Array Interactions
a = [1, 2, 3, 4]
b = [4, 5, 6]
#difference
a - b # [1, 2, 3]
#intersection
a & b # [4]
#Union
a | b # [1, 2, 3, 4, 5, 6] (every unique element)
a = [1, 2, 3, 4]
a.to_s # Returns "1234"
a.join('-') # Returns "1-2-3-4"
vowels = 'a e i o u'
vowels.split # => array
#each - executes block and returns the list of objects without mutating
# prints 246810 and returns [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5].each {|x| print x*2}
#map - executes block and returns the list of mutated objects
[1, 2, 3, 4, 5].map {|x| x*2} # => [2, 4, 6, 8, 10]
#select - returns a list of objects when condition is true
[1, 2, 3, 4, 5].select {|x| x==2} # => [2]
#reject - returns a list of objects when condition is false
[1, 2, 3, 4, 5].reject {|x| x==2} # => [1, 3, 4, 5]
#compact - return all non-nil objects
[1, 2, 3, nil, nil, 4].compact # => [1, 2, 3, 4]
#flatten - flatten inner arrays
[[3,2], [4,4]].flatten # => [3, 2, 4, 4]
#partition - Create two collections. First collection for true, second for false.
[1, 4, 5, 6].partition {|x| x==4||x==5} # => [[4, 5], [1, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment