ruby regular expression and rotate 2d array note

Nov 3, 2013 08:00 · 86 words · 1 minute read ruby regex

Regular Expression

char =~ /[0-9]/ or char =~ /\d/ or char =~ /[[:digit]]/.

These three expressions are the same to check the character is digit or not.

You can find more information from Ruby regular expressions

Rotate 2d Array

array.map { |row| row[0]} will return an array includes all first elements of each row.

So, we can use a loop to wrap this then to accomplish rotating a 2d array

result = []
0.upto(array.size - 1) do |i|
  result << array.map { |row| row|i| }
end