Recently I have come across a very interesting thing while working with Ruby arrays. My goal was to convert the array into a SQL query. For example, converting the array [[1,2,3],[4,5,6,],[7,8,9,]] into the string "(1,2,3),(4,5,6),(7,8,9)". If one has any basic knowledge about iterators, it won’t be a difficulty at all.
`map` returns new array which we join using "," string.
Also, you can put the data into SQL query manually. For example, adding any random integer values to our data:
However, pay attention! Of course, `map` does return the new array, but you, probably, think that the `a << rand (100)` adds the new value to the output array only. You are wrong! The thing is that when you work with `a` inside the `map` iterator, you work with the basic array, as by default, the variables are passed by link in Ruby. Therefore, when the new data is added in the existing array, you will get the following:
To create a new instance you must use the operation like ‘+’
Thus, don’t forget that variables in ruby are passed by link and when you work with ruby iterators you work with the basic data.