Array:
Ruby arrays or List are ordered, integer-indexed collections of any object. Each element in an array is associated with and referred to by an index.Array indexing starts at 0, as in C or Java. A negative index is assumed relative to the end of the array --- that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.
Ruby arrays can hold objects such as String, Integer, Fixnum, Hash, Symbol, even other Array objects. Ruby arrays are not as rigid as arrays in other languages. Ruby arrays grow automatically while adding elements to them.
Creating Arrays:
There are many ways to create or initialize an array. One way is with the new class method:names = Array.newYou can set the size of an array at the time of creating array:
names = Array.new(20)The array names now has a size or length of 20 elements. You can return the size of an array with either the size or length methods:
#!/usr/bin/ruby names = Array.new(20) puts names.size # This returns 20 puts names.length # This also returns 20This will produce the following result:
20 20You can assign a value to each element in the array as follows:
#!/usr/bin/ruby names = Array.new(4, "mac") puts "#{names}"This will produce the following result:
macmacmacmac
You can also use a block with new, populating each element with what the block evaluates to:#!/usr/bin/ruby nums = Array.new(10) { |e| e = e * 2 } puts "#{nums}"This will produce the following result:
024681012141618
There is another method of Array, []. It works like this:nums = Array.[](1, 2, 3, 4,5)One more form of array creation is as follows :
nums = Array[1, 2, 3, 4,5]The Kernel module available in core Ruby has an Array method, which only accepts a single argument. Here, the method takes a range as an argument to create an array of digits:
#!/usr/bin/ruby digits = Array(0..9) puts "#{digits}"This will produce the following result:
0123456789
Array Built-in Methods:
We need to have an instance of Array object to call a Array method. As we have seen, following is the way to create an instance of Array object:Array.[](...) [or] Array[...] [or] [...]This will return a new array populated with the given objects. Now, using created object, we can call any available instance methods. For example:
#!/usr/bin/ruby digits = Array(0..9) num = digits.at(6) puts "#{num}"This will produce the following result:
6
Hashes:
A Hash is a collection of key-value pairs like this: "employee" =>
"salary". It is similar to an Array, except that indexing is done via
arbitrary keys of any object type, not an integer index.
The order in which you traverse a hash by either key or value may seem arbitrary and will generally not be in the insertion order. If you attempt to access a hash with a key that does not exist, the method will return nil.
The order in which you traverse a hash by either key or value may seem arbitrary and will generally not be in the insertion order. If you attempt to access a hash with a key that does not exist, the method will return nil.
Creating Hashes:
As with arrays, there is a variety of ways to create hashes. You can create an empty hash with the new class method:months = Hash.newYou can also use new to create a hash with a default value, which is otherwise just nil:
months = Hash.new( "month" ) or months = Hash.new "month"When you access any key in a hash that has a default value, if the key or value doesn't exist, accessing the hash will return the default value:
#!/usr/bin/ruby months = Hash.new( "month" ) puts "#{months[0]}" puts "#{months[72]}"This will produce the following result:
month
month
#!/usr/bin/ruby H = Hash["a" => 100, "b" => 200] puts "#{H['a']}" puts "#{H['b']}"This will produce the following result:
100 200You can use any Ruby object as a key or value, even an array, so following example is a valid one:
[1,"jan"] => "January"
Hash Built-in Methods:
We need to have an instance of Hash object to call a Hash method. As we have seen, following is the way to create an instance of Hash object:Hash[[key =>|, value]* ] or Hash.new [or] Hash.new(obj) [or] Hash.new { |hash, key| block }This will return a new hash populated with the given objects. Now using created object we can call any available instance methods. For example:
#!/usr/bin/ruby $, = ", " months = Hash.new( "month" ) months = {"1" => "January", "2" => "February"} keys = months.keys puts "#{keys}"This will produce the following result:
2, 1
No comments:
Post a Comment