facets icon indicating copy to clipboard operation
facets copied to clipboard

String#indent not working according to demo :bug:

Open anithri opened this issue 9 years ago • 3 comments

String#tab is an alias for String#indent. But there are separate tests and demos maintained for both. The tab demo states Unlike #tabto, #tab indents all lines equally regardless of prior indention.. Followed by:

a = "abc\n  xyz".tab(2)
a.assert == "  abc\n  xyz" 
# Actually returns 
#> "  abc\n    xyz"

anithri avatar Feb 25 '15 04:02 anithri

That one took me a bit to figure out. There was a major transition in these methods starting in mid 2012 and culminating in Jan 2014. In short, it went something like this:

  1. tab becomes the same as tabto.
  2. tab is changed to be slightly different than tabto.
  3. margin gets renamed to trim.
  4. margin becomes a new method handling both tab and tabto, based on :lead option.
  5. tabto becomes an alias for margin (albeit the :lead is inverted, so it works like tab in No. 2)
  6. tab becomes just an alias for indent, but is to be deprecated.

It was point No. 1 that actually caused that demo to go afoul. tab was originally just defined as:

def tab(n)
  gsub(/^ */, ' ' * n)
end

I don't recall why I got rid of that altogether now. Guess I thought that was too simple to require a method. Perhaps it would make sense to reintroduce this definition as a new method, or perhaps as a different option of margin, or even indent (though that would require changing its interface to (n, opts={})). What do you think?

In any case, the tab alias can be deprecated for good now. And while we are at it tabto as well. I had a vague notion that in the future that I might try to write a new tab and possibly tabto that actually handled \t insertions properly (think of it as indent meets go's fmt).

trans avatar Mar 02 '15 23:03 trans

P.S. Here's the difference between margin with and without :lead. We need to make sure this is tested.

a = "    abc\n  xyz"
a.margin(2)
=> "    abc\n  xyz"
a.margin(2, :lead=>true)
=> "  abc\nxyz"
a.margin(4)
=> "      abc\n    xyz"
a.margin(4, :lead=>true)
=> "    abc\n  xyz"

trans avatar Mar 02 '15 23:03 trans

Unless there are any objections I will close this.

trans avatar Oct 26 '16 16:10 trans