I have written a method in Ruby to find all the circular combination of a text
x = "ABCDE"
(x.length).times do
puts x
x = x[1..x.length] + x[0].chr
end
Is there a better way to implement this ?
Here's an alternative approach.
str = "ABCDE"
(0...str.length).collect { |i| (str * 2)[i, str.length] }
I used a range and #collect
with the assumption that you'll want to do something else with the strings (not just print them).
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