In Tcl, if you have a list with even elements, one can loop two elements at a time. See this code:
foreach { a b } [ list 1 2 3 4 ] {
puts "${a} ${b}"
}
Will output:
1 2
3 4
How can one get the same behavior with Ruby?
You can get the same output by this way:
[1,2,3,4].each_slice(2) {|a, b| puts "#{a} #{b}" }
puts [1,2,33,44].join(' ').scan /\d+\s\d+/
1 2
33 44
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With