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 opposed 0
Opposed 0
Neutral 1
In favor 0
Strongly advocate 4
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .