You can achieve this in Ruby by using the each_cons method of the Enumerable module to generate all the contiguous substrings of length n from a given string.
Here's a code example:
main.rb164 chars8 linesIn the code above, the each_char method is used to iterate over each character of the string. The each_cons method is then used to generate consecutive arrays of n characters, which are then mapped to join them into substrings.
Running the example code with the string "1234567890" and n as 3 will output all the contiguous substrings of length 3 in the order they appear:
main.rb32 chars9 lines
Note that if the length n is greater than the length of the string, it will not output anything.
gistlibby LogSnag