Download the ExamplesArrays
Ruby has Arrays and Hashes to store/collect the objects.
And these objects are accessed using an index.
Array:
1. It is an ordered collection of objects.
2. Indexed with integers.
How to create an Array?
Answer is there are three different ways to create an array.
# Way 1 :
a1 = []
# Crosscheck whether we created the array "a1".
puts "a1 is an #{a1.class}"
# Way 2 :
a2 = [1,2,3]
# Crosscheck whether we created the array "a2".
puts "a2 is an #{a2.class}"
# Way 3 :
a3 = Array.new
# Crosscheck whether we created the array "a3".
puts "a3 is an #{a3.class}"
# Note:
# --> The array holds the objects within square brackets.
# --> The multiple objects separated with commas.
# --> The array holds different objects (int, float, string...).
# --> Way 1, creates an empty array.
# --> Way 2, creates an array with few objects defined/collected.
# --> Way 3, creates an empty array using ".new" operator/method.
# --> At any point to check whether we created array's or not, we can use ".class".
Exercise:
Well following are the built in methods that we can apply on to arrays.
&, *, +, -, <<, <=> , ==, [], [], []=, abbrev, assoc, at, clear, collect, collect!, compact , compact!, concat , dclone , delete, delete_at, delete_if, each, each_index, empty? , eql?, fetch , fill, first, flatten , flatten!, frozen? , hash include?, index , indexes, indices, initialize_copy , insert, inspect , join, last, length, map , map!, new, nitems , pack , pop, pretty_print, pretty_print_cycle, push , quote, rassoc, reject , reject!, replace, reverse, reverse! , reverse_each , rindex , select, shift, size , slice, slice!, sort , sort!, to_a , to_ary , to_s , to_yaml, transpose, uniq , uniq! , unshift, values_at, yaml_initialize, zip , . Download the ExamplesHashes :
Ruby has Arrays and Hashes to store/collect the objects.
And these objects are accessed using an index.
# How to create an Hash?
# Answer is there are three different ways to create an Hash.
# Way 1 :
h1 = {}
# Crosscheck whether we created the Hash "h1".
puts "h1 is a #{h1.class}"
# Way 2 :
h2 = {
"Object1"=>"First Object",
"Object2"=>"Second Object"
}
# Crosscheck whether we created the Hash "h2".
puts "h2 is a #{h2.class}"
# Way 3 :
h3 = Hash.new
# Crosscheck whether we created the Hash "h3".
puts "h3 is a #{h3.class}"
# Note:
# --> The hash hold objects within curly braces.
# --> The Key and Value pair forms an object in Hash.
# --> The multiple objects separated with commas.
# --> The hash holds different data types.
# --> Way 1, creates an empty Hash.
# --> Way 2, creates an Hash with few objects defined/collected.
# --> Way 3, creates an empty Hash using ".new" operator/method.
# --> At any point to check whether we created Hash or not, we can use ".class".
Exercise on Hashes:
==, [], [], []=, clear, default, default=, default_proc, delete, delete_if, each, each_key, each_pair, each_value, empty?, fetch, has_key?, has_value?, include?, index, indexes, indices, initialize_copy, inspect, invert, key?, keys, length, member?, merge, merge!, new, pretty_print , pretty_print_cycle, rehash, reject, reject!, replace, select, shift, size, sort, store, to_a, to_hash, to_s, to_yaml, update, value?, values, values_at, yaml_initialize
Download the ExamplesThe zip file contains examples on Creating Arrays and Hashes, Methods implemented on arrays.
Note:
- Please comment/un comment the wanted/un necessary puts, for better understanding.