Cut each N symbol from array What is the most efficient and productive way to cut each N symbol from a string?
For instance,

n = 5;
str = '1234A1234B1234C';
result: &quot;123412341234&quot;

This is my approach:
def delete_each_n(str, n)
  i = n

  str.length/n.times do
    str.slice!(i-1)
    i += (n - 1)
  end

  str
end
Your code

You have a sneaky bug in your code!

1000/2.times {|i| puts i }


You seem to think that this code would display 500 numbers between 0 and 499.
It doesn't. Instead, it displays 0, 1, and returns 500.

You need to replace str.length/n.times do with (str.length/n).times do.

Alternative

You can use gsub to look for the substrings, chop them and replace them:

def delete_every_nth_char(string, n)
  string.gsub(/.{#{n}}/){ |sub| sub.chop }
end

delete_every_nth_char('1234A1234B1234C', 5)
# "123412341234"
delete_every_nth_char('ABAB', 2)
# "AA"
delete_every_nth_char('ABA', 2)
# "AA"
delete_every_nth_char('ABA', 1)
# ""
delete_every_nth_char('ABA', 5)
# "ABA"


It is concise and probably faster than splitting and joining the strings manually.