From: shevegen@... Date: 2019-06-20T14:39:41+00:00 Subject: [ruby-core:93276] [Ruby trunk Feature#15945] Option to truncate in `String#ljust`, `String#rjust`, and `String#center` Issue #15945 has been updated by shevegen (Robert A. Heiler). I do not have any particular pro/con opinion per se as far as the feature itself is suggested; if anything then I am mostly neutral, perhaps slightly positive as I can see a (slight?) use case for it - I guess it depends a lot on the particular style the ruby user at hand uses. I use .center(), and especially .rjust() and .ljust() a lot in code where I have to print out something onto the terminal, so padding strings really is useful e. g. for "box layouts", like ASCII tables and such. I am even using this sometimes before making use of the "unicode box drawing" elements - so from this point of view, I can understand everyone else who is working in a somewhat similar way there. I agree that, while [start, end] position is simple and universal in ruby to use, it also indeed requires a little bit of thinking. I myself try to write ruby code in a way as to never have to think, if this can be avoided - that way I don't write great code, but I just don't have to think a lot, which is quite useful to me; I can stay lazy that way. So, from that point of view, options to not have to make me think are great. :) I assume that one reason why the second suggestion was made was because three parameters may be a bit more cumbersome to use than two or one. Changing the current behaviour may have to go past ruby 3.0, if it is added. So the first suggestion may be simpler to realize. As for "truncate" versus "trunc" - I think the longer name would be better. While being short and succinct can be great, in this context the slightly longer way may be better, as people may have an easier time understanding what it does in a "natural way", e. g.: trunc: true versus truncate: true (At the least if only those two options are given.) But mostly I am really neutral on this suggestion either way, just commenting on it. :) ---------------------------------------- Feature #15945: Option to truncate in `String#ljust`, `String#rjust`, and `String#center` https://bugs.ruby-lang.org/issues/15945#change-78734 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Sometimes, I need to adjust a string to an exact length: Pad if shorter, and truncate if longer. To do that, I need to combine two methods like this: ```ruby "12".ljust(5, "*")[0, 5] # => "12***" "1234567".ljust(5, "*")[0, 5] # => "12345" "xyz".rjust(5, "*")[-5, 5] # => "**xyz" "stuvwxyz".rjust(5, "*")[-5, 5] # => "vwxyz" ``` But that is messy, and needs a bit of thinking. It becomes even harder with centering. I request an option on `String#ljust`, `String#rjust`, `String#center` to truncate the string when it is longer than the given length. One way to do so may be: take a keyword `:truncate` or `:trunc`. ```ruby "12".ljust(5, "*", trunc: true) # => "12***" "1234567".ljust(5, "*", trunc: true) # => "12345" "xyz".rjust(5, "*", trunc: true) # => "**xyz" "stuvwxyz".rjust(5, "*", trunc: true) # => "vwxyz" "abc".center(5, "*", trunc: true) # => "*abc*" "abcdefg".center(5, "*", trunc: true) # => "bcdef" ``` Another way is, when the length is negative, interpret it as truncating option. ```ruby "12".ljust(-5, "*") # => "12***" "1234567".ljust(-5, "*") # => "12345" "xyz".rjust(-5, "*") # => "**xyz" "stuvwxyz".rjust(-5, "*") # => "vwxyz" "abc".center(-5, "*") # => "*abc*" "abcdefg".center(-5, "*") # => "bcdef" ``` But the second way changes the current behavior. -- https://bugs.ruby-lang.org/ Unsubscribe: