This is the old RCRchive. It's available only for reading and reference. To submit RCRs for Ruby 1.9/2.0, and to participate in discussions about them, please visit the new RCRchive.
ruby picture

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
ruby picture
Comments Current voting
Strongly opposed0
Opposed0
Neutral1
In favor0
Strongly advocate6
ruby picture
ruby picture

Powered by Ruby on Rails.