RCR 313: rjust, ljust, center split on seperator
Submitted by transami (Sun Aug 28 14:24:02 UTC 2005)
Abstract
Add some additional flexibitiy to String methods rjust, ljust and center.
Problem
Just noticed that I had to write my own methods to align each line of a string left, right or center, since rjust, ljust and center only do so for the whole of a string with no regard for line separation.
Proposal
Generalize the methods adding a separator parameter.
Analysis
Just seems like a logicial extension of the methods already there, rather then wasting namespace with addtional methods that do almost the same thing.
A minor incompatability arises if the deault seperator is "\n" as I think it likely would best be. Also perhpas the seperator parameter should come before the buffer string parameter.
(P.S. If otherwise rejected what would should the corresponding methods be called?)
Implementation
Example:
alias_method :rjust_whole, :rjust
def rjust(n,c=' ',sep="\n")
return rjust_whole(n,c) unless sep
q = split(sep.to_s).collect { |line|
line.rjust_whole(n,c)
}
q.join(sep.to_s)
end
|
Strongly opposed |
0 |
Opposed |
0 |
Neutral |
1 |
In favor |
0 |
Strongly advocate |
4 |
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .