Can some analyse this piece of code

Hi Guys,

Stumbled upon this piece of code and would like to know what it does

(0...8).map{65.+(rand(25))}.join

1 Like

Hi,

Iā€™m not a Ruby developper, but I challenched my self to understand this with my Javascript knowledge.

First, we need to explode this inline code.

first_part = (0...8)

second_part = first_part.map{65.+(rand(25))}

second_part.join 

The first part create an array of digits from 0 to 7.

The second part is a map that invokes block once for each element in the array.
In this case this will fill each element with 65.+(rand(25)).

65.+(rand(25)) is something like 65 added to a random number between 0 and 25.

So, the second_part array contains 8 elements with value between 65 and 90.

The lastpart is about to join this array in a single element.

['a', 'l', 'p', 'h', 'a'].join => 'alpha'
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.