Skip to main content Link Search Menu Expand Document (external link)

Why you need to know Array & Hash?

Most of programming languages need to handle data structure.

For example, Ruby can put many order into Array(we also say it’s a kind of container), then it can use some method to calculate and handle data.

Ruby’s arrays and hashes are indexed collections. Both store collections of objects, accessible using a key. With arrays, the key is an integer, whereas hashes support any object as a key. Both arrays and hashes grow as needed to hold new elements.

Table of contents

  1. Array
  2. Hash

Array

  1. A new array can be created by using the literal constructor []. Arrays can contain different types of objects
      irb(main):001:0> name = ["foo", "bar", "baz", "qux"]
      => ["foo", "bar", "baz", "qux"]
    

    Array example

  2. Access elements.

    The first element index is 0, other words name[1] is the SECOND element

      irb(main):001:0> name[0]
      => "foo"
      irb(main):002:0> name[1]
      => "bar"
      irb(main):001:0> name[2]
      => "baz"
      irb(main):001:0> name[3]
      => "qux"
      irb(main):001:0> name[4]
      => nil
    
  3. Print data by access array element
      irb(main):001:0> print "The first element is ", name[0], "! \n"
      The first element is foo!
      => nil
    
  4. Assign data into element
      irb(main):001:0> name = ["foo", "bar", "baz", "qux"]
      => ["foo", "bar", "baz", "qux"]
      irb(main):002:0> name[0] = "quux"
      => "quux"
      irb(main):003:0> name
      => ["quux", "bar", "baz", "qux"]
    
  5. Ruby will auto adjust array size.
      irb(main):001:0> name = ["foo", "bar", "baz", "qux"]
      => ["foo", "bar", "baz", "qux"]
      irb(main):002:0> name[4] = "quux"
      => "quux"
      irb(main):003:0> name
      => ["foo", "bar", "baz", "qux", "quux"]
    
  6. Array can assign every kinds of type object.
      irb(main):009:0> numbers = [1, 2, 3, 4, 5]
      => [1, 2, 3, 4, 5]
      irb(main):010:0> mixed = [1, 'foo', 2, 'bar', 3]
      => [1, "foo", 2, "bar", 3]
    
  7. Array has a method size will return how many elements in this array.
      irb(main):001:0> name = ["foo", "bar", "baz", "qux"]
      => ["foo", "bar", "baz", "qux"]
      irb(main):002:0> name.size
      => 4
    
  8. Array iterate by .each.
      irb(main):001:0> names = ["foo", "bar", "baz", "qux"]
      => ["foo", "bar", "baz", "qux"]
      irb(main):002:0> names.each do |name|
      irb(main):003:1* puts name
      irb(main):004:1> end
      foo                              -> from puts name
      bar                              -> from puts name
      baz                              -> from puts name
      qux                              -> from puts name
      => ["foo", "bar", "baz", "qux"]  -> Array call each method will return current array
    

Hash

Hash different from Array, Array use literal constructor, Hash uses key-value.

  1. Create a new Hash, Ruby always use symbol to be key, like :foo, :bar
    here has two kinds of code style below.
      irb(main):001:0> song = {title: 'Kumpas', artist: 'Moira Dela Torre'}  -> recommend
      => {:title=>"Kumpas", :artist=>"Moira Dela Torre"}
      irb(main):002:0> song = {:title => 'Kumpas', :artist => 'Moira Dela Torre'}
      => {:title=>"Kumpas", :artist=>"Moira Dela Torre"}
    

    Hash example

  2. Access elements, it’s almost same with array, just need to put key.
      irb(main):001:0> song = {title: 'Kumpas', artist: 'Moira Dela Torre'}
      => {:title=>"Kumpas", :artist=>"Moira Dela Torre"}
      irb(main):002:0> song[:title]
      => "Kumpas"
      irb(main):003:0> song[:artist]
      => "Moira Dela Torre"
      irb(main):004:0> song[:year]
      => nil
    
  3. Hash iterate by .each, hash in block with two variable, key and value. the usage almost same with Array.
      irb(main):001:0> song = {title: 'Kumpas', artist: 'Moira Dela Torre'}
      => {:title=>"Kumpas", :artist=>"Moira Dela Torre"}
      irb(main):002:0> song.each do |key, value|
      irb(main):003:1* puts "#{key}: #{value}"
      irb(main):004:1> end
      title: Kumpas
      artist: Moira Dela Torre
      => {:title=>"Kumpas", :artist=>"Moira Dela Torre"}
    

Back to top

Copyright © 2020-2022 Secure Smarter Service, Inc. This site is powered by KodaCamp.