I am just wondering if there is a better way to split string to substrings by N characters:
"something" (by 4 characters) -> ["some", "omet", "meth", "ethi", "thin", "hing"]
Here is my code:
n = 4
s = "something"
result = []
0.upto(s.size - n) do | idx |
result << s[idx..idx+n-1]
end
p result
1 Answer