tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.

Join the forum, it's quick and easy

tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.
tutorial net
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Admin
Admin
Posts : 207
Join date : 2017-11-11
Age : 33
Location : algeria
http://www.tutorial-net.com

tutorial ruby - Store your data with tables and hashes Empty tutorial ruby - Store your data with tables and hashes

Sun Apr 01, 2018 5:11 pm
You can group your data in two ways:

in tables : to order a series of elements using indexes;

in hashing tables : to find items easily using keys.

The tables
Tables allow you to arrange data in an orderly way that is found using an index .

To create an array, use the square brackets, for example:

mes_escales_tour_du_monde = [ "Paris", "Toronto", "NYC", "Rio", "Sydney", "Hong-Kong", "Berlin" ]
Does it tempt you ...? Wink

Methods can be applied  to a variable containing a table as follows:

nom_du_tableau.nom_de_la_methode
The method size allows to know the number of elements of an array. For example, to check how many stops I will make in my world tour, I can type:

mes_escales_tour_du_monde.size
which will return the value 7.

The method is reverse used to invert the order of the elements of an array. So, if I decide to go around the world in the opposite direction, I can do:

mes_escales_tour_du_monde.reverse
which will return an array Berlin first stop:  ["Berlin","Hong-Kong", ....].

To access an element in a table, specify the position (or index) of the element in square brackets. For example,

mes_escales_tour_du_monde[2]
will return the city of "NYC".

The first element in an array has index 0 (and not 1)! This is also the case in many programming languages.  

To add an element to a table, use two chevrons <<. If I want to extend my tour of the world in London, I will type:

mes_escales_tour_du_monde << "Londres"
To modify the element of an array, it is assigned the desired value with the sign =. So I can replace Berlin with Barcelona if I speak Spanish better than German:

mes_escales_tour_du_monde[6] = "Barcelone"
The tables of hashing
The hash tables (or hashes ) used to store data that can be found using a key .

To create a hash table, we use braces {}. Each value stored in this table is associated with a key that will find it:

Code:
table_de_hashage = { clé_1: valeur_1, clé_2: valeur_2, ... }
For example, to record the number of days to spend in each stop, I can create a hash table jours_voyage  :

jours_voyage = { paris: 0, toronto: 7, nyc: 3 }
To access the value of an element in a hash, we specify its key in square brackets. If I do not remember how long I have planned for my stopover in Toronto, just call:

jours_voyage[:toronto]
who will send me back 7 days.

To add or modify an element in a hash, use the = operator. For example by adding:  

Code:
jours_voyage[:rio] = 5
my hash jours_voyage will contain the duration of my stay in Rio plus:

Code:
{ paris: 0, toronto: 7, nyc: 3, rio: 5 }
Back to top
Permissions in this forum:
You cannot reply to topics in this forum