Here are a few quick examples :
Arrays are ordered and indexed collections.First, initialize a new array. Add some elements to it (1,2,3). Remove an element (1). If you add multiple copies of 1, then all instances of 1 are deleted if you do arr0 -= [1] irb(main):001:0> arr0 = Array.new => [] irb(main):002:0> arr0 += [1,2,3] => [1, 2, 3] irb(main):003:0> arr0 -= [1] => [2, 3] irb(main):004:0> arr0 += [1,1,1] => [2, 3, 1, 1, 1] irb(main):005:0> arr0 -= [1] => [2, 3] Other ways in which new arrays can be defined. Much of this is syntactical sugar. irb(main):006:0> arr1 = [1,2,3,4,5] => [1, 2, 3, 4, 5] irb(main):009:0> arr2 = ["ABC","DEF","GHI"] => ["ABC", "DEF", "GHI"] irb(main):010:0> arr3 = Array.new(10){|x| x*x} # Declaring an array where a[i] = i*i => [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] irb(main):013:0> arr4 = Array["ab","de"] => ["ab", "de"] Sorting an array :irb(main):023:0> arr5 = [100,200,300,50,40,0,1,2,199,204,3403,506]
=> [100, 200, 300, 50, 40, 0, 1, 2, 199, 204, 3403, 506]
irb(main):026:0> arr6 = arr5.sort # This gives us a new array which is a sorted version of arr5
=> [0, 1, 2, 40, 50, 100, 199, 200, 204, 300, 506, 3403]
irb(main):027:0> arr5 # We verify that arr5 is still unchanged
=> [100, 200, 300, 50, 40, 0, 1, 2, 199, 204, 3403, 506]
irb(main):028:0> arr5.sort! #sort with an exclamation mark -> this will do an in-place sort on arr5
=> [0, 1, 2, 40, 50, 100, 199, 200, 204, 300, 506, 3403] Sorting an array with a comparator function of our choice. For this we use the sort_by method offered by enumerable. Let's say, we want to sort an array of integers, in ascending order of f(x) where f(x) = x^2 - x^3 + x + 20. irb(main):029:0> arr = [0,1,2,3,4,5,6,7,8,9,10] # Let this be our original Array => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] irb(main):030:0> def f(x) # This is the function f(x) irb(main):031:1> x * x - x * x * x + x + 20 irb(main):032:1> end => nil irb(main):034:0> arrSpecialSort = arr.sort_by{|a| f(a)} # Elements sorted in ascending order of their corresponding f(a) values => [10, 9, 8, 7, 6, 5, 4, 3, 2, 0, 1] # I want to make sure and check that the values of our special-sorted array are actually in ascending order of f(a) values irb(main):036:0> arrSpecialSort .map{|a| f(a)} => [-870, -619, -420, -267, -154, -75, -24, 5, 18, 20, 21] # The last array shows the f(a) values corresponding to each value of our sorted array. Yes, arrSpecialSort was sorted correctly. Selecting specific elements from an array.Suppose we have an array of integers. And we want to select only those which are even. We can use the "select" method over here. Select, transforms a given array into one which only contains the elements which satisfy a certain condition (or predicate). irb(main):038:0> arr = [1,2,3,4,5,6,7,8,9,10] => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] irb(main):039:0> arr.select{|x| x%2 == 0} # Selecting only those elements which are even => [2, 4, 6, 8, 10] irb(main):040:0> arr.select{|x| x%2 == 1} # Selecting only those elements which are odd => [1, 3, 5, 7, 9] arr.select!{|x| x%2 == 1} => [1, 3, 5, 7, 9]
Applying a function to each element of a list. This can be done using the map method. Suppose, we want a new array, such that, for every element, x in the original array, the new array contains 2x+10. Here's how we could go about this : irb(main):043:0> arr = [0,2,3,4,6,7,8] => [0, 2, 3, 4, 6, 7, 8] irb(main):044:0> arr.map{|x| 2*x + 10 } => [10, 14, 16, 18, 22, 24, 26] Multi-Dimensional Arrays in RubyHere's how you could initialize multi-dimensional array in Ruby. The multi-dimensional array, as initialized below, has 10 rows and 5 columns, all initialized to zero. irb(main):048:0> x = Array.new(10) {Array.new(5){0}} => [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] irb(main):049:0> x[1][1] => 0
irb(main):050:0> x[2][2]=20 => 20 Or you could use 'matrix' which is a package bundled with Ruby. irb(main):051:0> require 'matrix' => true irb(main):052:0> m = Matrix[ [0,1,2],[2,3,4]]
=> Matrix[[0, 1, 2], [2, 3, 4]] irb(main):056:0> m.element(0,1) # Accessing a particular element => 1 irb(main):057:0> m.column(1) # Accessing a particular column
=> Vector[1, 3] irb(main):058:0> m.row(1) # Accessing a particular row
=> Vector[2, 3, 4] Check out some of our other Ruby Tutorials : Introduction to RubyBasic Data Structures With Ruby | ![]() Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb) Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case Introduction to Ruby Comments - Single and Multi-Line comments Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays Introduction to Ruby - Strings Introduction to Ruby - Making a Script Executable Introduction to Ruby - Regular Expressions, Match, Scan Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion Introduction to Ruby - Computing a Power Set : An Example of Recursion Introduction to Ruby - Towers of Hanoi : An Example of Recursion Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods Basic Data Structures in Ruby - Insertion Sort Basic Data Structures in Ruby - Selection Sort Basic Data Structures in Ruby - Merge Sort Basic Data Structures in Ruby - Quick Sort Functional Programming with Ruby Basic Data Structures in Ruby - Stack Basic Data Structures in Ruby - The Queue Basic Data Structures in Ruby - Linked List - ( A Simple, Singly Linked List) |
Computer Science >