almost 9 years ago
Ruby array is a list of elements that can be of any type either string or integer. Hence, In ruby it can be integer or string.
we can declare array by placing the elements inside the brackets like;-
you can see the above declared array has string, integer and float. In ruby array can have any data type inside them.
now lets focus on how to fetch the value of array, if you want to fetch the first value of array then we can do like this
if you want to fetch the value by indexing
How to Modify existing Array
Once we declare array we can also modify its elements, we can do that in 2 ways.
push is used to add extra element inside the array.
pop is used to remove last element from an array.
it will remove last element from the array.
Ruby hashes are the data structure in which we store items by keys, entries in hash are stored in a key value pair where keys are created as symbol and value is created as data type.
this is the older syntax of hash in ruby.
Now a days we usually use newer syntax:
you can have multiple of key value pairs inside one hash.
if you want to add new key value pair to existing hash you can do like this
it will add new element inside a { a hash } with :office as a key and 'Gurgaon' as value.
you can also delete an element from an existing hash
it will delete id from existing {hash a }
0 Comment(s)