The thing I love about Ruby is its elegance: if we use inject or map along with take_while and select, we can chain blocks together to achieve a lot while writing little.
Sticking with the idea of single line solutions, how would one write a nested for loop in Ruby without writing the entire nested for loop? I feel it must be possible, I just can't for the life of me figure out what it is. I am looking for something like this:
10.times {|a| 10.times {|b| a*b}} The only solution I can come up with that is at all elegant is nested for loops. Does anyone have a better solution?
array = [] for a in (1..10) for b in (1..10) array << a*b end end
<<a*bstring as HEREDOC syntax instead of<<and thena*b. A single space is required between them to separate the tokens and avoid the parse error.