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
Array
- 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"]
- Access elements.
The first element
index
is0
, other wordsname[1]
is the SECOND elementirb(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
- Print data by access array element
irb(main):001:0> print "The first element is ", name[0], "! \n" The first element is foo! => nil
- 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"]
- 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"]
- 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]
- 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
- 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.
- 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"}
- 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
- Hash iterate by
.each
, hash in block with two variable,key
andvalue
. 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"}