While working through a Ruby problem on Codewars, I discovered a way to combine an array of integers in order to work on it as a string. As an example, see the following code snippet:
[3,2,1].join # --> returns "321"
This was surprising to me. In order to get the same result, I had taken the long route:
str = ""
[3,2,1].each { |digit| string << digit.to_s }
str.to_i
With Ruby, as often is the case, there’s a lot concealed underneath the .join
method, but it’s a nice little shortcut for sure. One to remember…