Ljust, rjust

Just thought I would run these ideas by everyone:

How about allowing ljust' andrjust’ take an additional parameter (which
defaults to the string " ") which specifies what it justifies with?

15.to_s.rjust(5) # --> " 15"
15.to_s.rjust(5,‘0’) # --> "00015"
15.to_s.rjust(5,‘blue’) # --> “blu15”

What do you think? (Mostly, I just want to pad numbers with leading zeros.
Yes, of course I can change `rjust’ myself, yadda-yadda… but then there’s
that “you really shouldn’t modify the core classes” thing. Doesn’t seem
quire right, does it?)

Chris

I usually do

sprintf “%05d”, 15
=> “00015”
sprintf “%05d”, “15”
=> “00015”

How about making ‘sprintf’ so that you can write:

“15”.sprintf “%05d” # → “00015”

Daniel.

···

On Fri, May 02, 2003 at 02:35:10AM +0900, Chris Pine wrote:

Just thought I would run these ideas by everyone:

How about allowing ljust' and rjust’ take an additional parameter (which
defaults to the string " ") which specifies what it justifies with?

15.to_s.rjust(5) # → " 15"
15.to_s.rjust(5,‘0’) # → “00015”
15.to_s.rjust(5,‘blue’) # → “blu15”

What do you think? (Mostly, I just want to pad numbers with leading zeros.
Yes, of course I can change `rjust’ myself, yadda-yadda… but then there’s
that “you really shouldn’t modify the core classes” thing. Doesn’t seem
quire right, does it?)

Chris


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

I am with you on this one, I’ve been needing this functionality for some
time now … I think it would extend [lr]just’s functionality in a very
natural way (I was really surprised when I discover it didn’t do that).

Simon

···

On Thu, 01 May 2003 at 17:35 GMT, Chris Pine wrote:

How about allowing ljust' and rjust’ take an additional parameter (which
defaults to the string " ") which specifies what it justifies with?


There are 10 types of people in the world…
those who understand binary and those who don’t.

I am with you on this one, I’ve been needing this functionality for some
time now … I think it would extend [lr]just’s functionality in a very
natural way (I was really surprised when I discovered it didn’t do that).

Simon

···

On Thu, 01 May 2003 at 17:35 GMT, Chris Pine wrote:

How about allowing ljust' and rjust’ take an additional parameter (which
defaults to the string " ") which specifies what it justifies with?


There are 10 types of people in the world…
those who understand binary and those who don’t.

Saluton!

How about allowing ljust' and rjust’ take an additional parameter (which
defaults to the string " ") which specifies what it justifies with?

15.to_s.rjust(5) # → " 15"
15.to_s.rjust(5,‘0’) # → “00015”
15.to_s.rjust(5,‘blue’) # → “blu15”

If that is to be implemented one needs to define what
15.10.to_s.rjust(10,'ppm ') should result in because there are two
possiblities: ‘ppm pp15.1’ or ‘ppm 15.1’ - both makes sense.

Gis,

Josef ‘Jupp’ Schugt

···

/“”“”\ e-mails that do not contain plain text, are larger than |
/ STOP \ 50 KiB, are unsolicited, or contain binarys are ignored |
\ SPAM / unless payment from your side or technical reasons give |
____/ rise to a non-standard treatment. ______________________|

How about allowing ljust' and rjust’ take an
additional parameter (which defaults to the string
" ") which specifies what it justifies with?

15.to_s.rjust(5) # → " 15"
15.to_s.rjust(5,‘0’) # → “00015”
15.to_s.rjust(5,‘blue’) # → “blu15”

What do you think?

I agree wholeheartedly.  The problem is made even worse by the fact that

‘%05s’ does not pad with leading zeros. I have had to get around this
shortfall by doing things like:

foo.to_s.rjust(10).sub(/^ {,10}/,‘0’)

Not only is this not very readable, but it is also fairly inefficient.

I also find the lack of a padding string parameter very surprising since

every other language I have run across that implements padding functions
allows (or even requires) you to specify the padding string. If you don’t
believe me, do a Google search on “lpad function” and just try to find one
that doesn’t :o)

All in all, this would seem to be a real benefit to the language, is

easy to implement, and should not break any existing code.

To expedite things, I have attached a patch that unifies the three

functions (ljust, rjust, and center) and adds this functionality. The patch
can be applied to last night’s snapshot. I have performed a moderate amount
of testing on this patch and it seems OK.

One quirk: a zero-length second parameter like "'x'.ljust(10,nil)" or

“‘x’.ljust(10,‘’)” behaves like “‘x’.ljust(10)”.

A generalized "justify" function similar to the one implemented in this

patch might be a nice addition to the language too.

- Warren Brown

string.c.diff (4.26 KB)

I am with you on this one, I’ve been needing this functionality for some
time now … I think it would extend [lr]just’s functionality in a very
natural way (I was really surprised when I discover it didn’t do that).

Simon

···

On Thu, 01 May 2003 at 17:35 GMT, Chris Pine wrote:

How about allowing ljust' and rjust’ take an additional parameter (which
defaults to the string " ") which specifies what it justifies with?


There are 10 types of people in the world…
those who understand binary and those who don’t.

We already have something like this… it just
happens to be backwards.

“%05d” % “15” # “00015”

But this doesn’t address the original poster’s
desire for generality, I guess.

Hal

···

----- Original Message -----
From: “Daniel Carrera” dcarrera@math.umd.edu
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, May 01, 2003 12:55 PM
Subject: Re: ljust, rjust…

I usually do

sprintf “%05d”, 15
=> “00015”
sprintf “%05d”, “15”
=> “00015”

How about making ‘sprintf’ so that you can write:

“15”.sprintf “%05d” # → “00015”

One quirk: a zero-length second parameter like “‘x’.ljust(10,nil)”
or “‘x’.ljust(10,‘’)” behaves like “‘x’.ljust(10)”.

Will it handle “‘x’.ljust(10, ‘\0’)” properly?

A generalized “justify” function similar to the one implemented in
this patch might be a nice addition to the language too.

You may want to look at Text::Format (in the RAA) for some of this.

-austin
– Austin Ziegler, austin@halostatue.ca on 2003.05.01 at 21:49:20

···

On Fri, 2 May 2003 10:18:36 +0900, Warren Brown wrote:

Hi,

···

In message “[PATCH] ljust, rjust, and center with pad string (was ljust, rjust…)” on 03/05/02, “Warren Brown” wkb@airmail.net writes:

How about allowing ljust' and rjust’ take an
additional parameter (which defaults to the string
" ") which specifies what it justifies with?

15.to_s.rjust(5) # → " 15"
15.to_s.rjust(5,‘0’) # → “00015”
15.to_s.rjust(5,‘blue’) # → “blu15”

What do you think?

I agree wholeheartedly.

It’s planned for long time. The only reason we don’t have them yet is
me being to lazy. But now, we have implementation. We need to wait
no longer. Thank you, Warren.

And Austin, it seems to work with '\0’s.

						matz.