Skip to content

Instantly share code, notes, and snippets.

@windwiny
Last active May 6, 2019 01:47
Show Gist options
  • Save windwiny/b823832bf13d936bf2a506828f2fa749 to your computer and use it in GitHub Desktop.
Save windwiny/b823832bf13d936bf2a506828f2fa749 to your computer and use it in GitHub Desktop.
expandtabs ruby
class String
# like python expandtabs
def expandtabs tabsize=8
self.gsub(/[^\r\n]*/) do |line|
next line if line == ''
ss = line.split("\t", -1)
next line if ss.size == 1
sa = []
lasts = ss.pop
ss.each do |s|
sa << s
m = s.b.size % tabsize # FIXME. graph width 2, but utf-8 byte 3/4
sa << ' ' * ( tabsize - m )
end
sa << lasts
sa.join
end
end unless defined? expandtabs
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment