DEV Community

Charlie @ 90-10.dev
Charlie @ 90-10.dev

Posted on • Originally published at 90-10.dev

An introduction to Ruby Arrays

An array is an ordered collection of objects; they can be of any type and you can intermix different types inside a particular array. To create an array use square brackets - below, we're instantiating an array containing an integer, a string and a float:

an_array = [1, 'two', 3.0]
Enter fullscreen mode Exit fullscreen mode

Element indexes

Just like in other programming languages, arrays in Ruby are 0-indexed - belowe we're printing the second element of the array:

an_array[1]  
  two
Enter fullscreen mode Exit fullscreen mode

Requesting an index that doesn't exist will return a nil value:

an_array[3]  
  nil
Enter fullscreen mode Exit fullscreen mode

Indexes can also be negative and they are used to transverse the array in a reverse order:

an_array[-1]  
  3.0
Enter fullscreen mode Exit fullscreen mode
an_array[-3]  
  1
Enter fullscreen mode Exit fullscreen mode

Array slices

You can return a part (or slice) of an array by providing a range with the start and end index:

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
letters[2..5]  
  ['c', 'd', 'e', 'f']
Enter fullscreen mode Exit fullscreen mode

NB: Ranges are a basic Ruby type.

To retrieve a slice containing the elements from a certain index, don't specify an end index:

letters[5..]  
  ['f, 'g', 'h', 'i', 'j']
Enter fullscreen mode Exit fullscreen mode

Basic array methods

Using the letters arrays above, let's apply some methods to it - we'll start with its length:

letters.length  
  10
Enter fullscreen mode Exit fullscreen mode

NB:2 aliasses are available for length - size and count will work just as well.

Ruby methods are named in a very intuitive way following the principle of least suprise. It should be very easy for you to understand the methods below:

letters.empty?
  false

letters.sort
  ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

letters.reverse
  ["j", "i", "h", "g", "f", "e", "d", "c", "b", "a"]

letters.min
  "a"

letters.max
  "j"


Enter fullscreen mode Exit fullscreen mode

To view all methods available for our array, use the methods method:

letters.methods

  [:to_h, :include?, :&, :*, :+, :-, 
  :at, :fetch, :last, :union, :difference, :intersection, 
  :push, :append, :pop, :shift, :unshift, :each_index, :join, 
  :rotate, :rotate!, :sort!, :sort_by!, :collect!, :map!, 
  :select!, :filter!, :keep_if, :values_at, :delete_at, 
  :delete_if, :reject!, :transpose, :fill, :assoc, :rassoc, 
  :uniq!, :compact, :compact!, :flatten, :flatten!, :shuffle!, 
  :shuffle, :sample, :permutation, :combination, 
  :repeated_permutation, :repeated_combination, :product, 
  :bsearch, :sort, :bsearch_index, :deconstruct, :count, 
  :find_index, :select, :filter, :reject, :collect, :map, 
  :first, :all?, :any?, :one?, :none?, :minmax, :|, 
  :reverse_each, :zip, :take, :take_while, :drop, :<=>, :<<, 
  :cycle, :drop_while, :==, :sum, :uniq, :[], :[]=, :insert, 
  :empty?, :eql?, :index, :rindex, :replace, :clear, :max, 
  :min, :inspect, :length, :size, :each, :reverse, :concat, 
  :prepend, :reverse!, :to_ary, :to_a, :to_s, :pack, :delete, 
  :slice, :slice!, :dig, :hash, :each_slice, :each_cons, 
  :each_with_object, :chunk, :slice_before, :slice_after, 
  :slice_when, :chunk_while, :to_set, :chain, :lazy, :find, 
  :entries, :sort_by, :grep, :grep_v, :detect, :find_all, 
  :filter_map, :flat_map, :collect_concat, :inject, :reduce, 
  :partition, :group_by, :tally, :min_by, :max_by, :minmax_by, 
  :member?, :each_with_index, :each_entry, :dup, :itself, 
  :yield_self, :then, :taint, :tainted?, :untaint, :untrust, 
  :untrusted?, :trust, :frozen?, :methods, :singleton_methods, 
  :protected_methods, :private_methods, :public_methods, 
  :instance_variables, :instance_variable_get, 
  :instance_variable_set, :instance_variable_defined?, 
  :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, 
  :tap, :class, :display, :singleton_class, :clone, 
  :public_send, :method, :public_method, :singleton_method, 
  :define_singleton_method, :extend, :to_enum, :enum_for, 
  :===, :=~, :!~, :nil?, :respond_to?, :freeze, :object_id, 
  :send, :__send__, :!, :!=, :__id__, :equal?, :instance_eval, 
  :instance_exec] 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)