# Ljust, rjust

**URL:** <https://rubytalk.org/t/ljust-rjust/5838>\
**Category:** ruby-talk\
**Created:** [1 May 2003 17:35 UTC](https://rubytalk.org/t/ljust-rjust/5838 "2003-05-01T17:35:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris18](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Chris18](https://rubytalk.org/u/Chris18)\
**Post date:** [1 May 2003 17:35 UTC](https://rubytalk.org/t/ljust-rjust/5838/1 "2003-05-01T17:35:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrera](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Daniel\_Carrera](https://rubytalk.org/u/Daniel_Carrera)\
**Post date:** [1 May 2003 17:55 UTC](https://rubytalk.org/t/ljust-rjust/5838/2 "2003-05-01T17:55:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Simon\_Vandemoortele1](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Simon\_Vandemoortele1](https://rubytalk.org/u/Simon_Vandemoortele1)\
**Post date:** [1 May 2003 18:42 UTC](https://rubytalk.org/t/ljust-rjust/5838/3 "2003-05-01T18:42:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Simon\_Vandemoortele1](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Simon\_Vandemoortele1](https://rubytalk.org/u/Simon_Vandemoortele1)\
**Post date:** [1 May 2003 18:42 UTC](https://rubytalk.org/t/ljust-rjust/5838/4 "2003-05-01T18:42:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Josef\_Jupp\_SCHUGT](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Josef\_Jupp\_SCHUGT](https://rubytalk.org/u/Josef_Jupp_SCHUGT)\
**Post date:** [1 May 2003 22:40 UTC](https://rubytalk.org/t/ljust-rjust/5838/5 "2003-05-01T22:40:36Z")

</div>

Saluton!

- Chris Pine [nemo@hellotree.com](mailto:nemo@hellotree.com); 2003-05-01, 20:55 UTC:

> 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. \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_|

---

<div class="post-metadata">

**Author:** ![Warren\_Brown3](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@Warren\_Brown3](https://rubytalk.org/u/Warren_Brown3)\
**Post date:** [2 May 2003 01:18 UTC](https://rubytalk.org/t/ljust-rjust/5838/6 "2003-05-02T01:18:36Z")

</div>

> 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](https://rubytalk.org/uploads/short-url/h8J68Eqk2uFA3BqfUjaXhWz4W3U.diff) (4.26 KB)

---

<div class="post-metadata">

**Author:** ![Simon\_Vandemoortele1](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@Simon\_Vandemoortele1](https://rubytalk.org/u/Simon_Vandemoortele1)\
**Post date:** [15 October 2003 03:26 UTC](https://rubytalk.org/t/ljust-rjust/5838/7 "2003-10-15T03:26:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [1 May 2003 17:58 UTC](https://rubytalk.org/t/ljust-rjust/5838/8 "2003-05-01T17:58:36Z")

</div>

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](mailto:dcarrera@math.umd.edu)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto: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”

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)\
**Post date:** [2 May 2003 01:51 UTC](https://rubytalk.org/t/ljust-rjust/5838/9 "2003-05-02T01:51:31Z")

</div>

> 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:

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [2 May 2003 02:54 UTC](https://rubytalk.org/t/ljust-rjust/5838/10 "2003-05-02T02:54:35Z")

</div>

Hi,

> **···**
>
> In message “[PATCH] ljust, rjust, and center with pad string (was ljust, rjust…)” on 03/05/02, “Warren Brown” [wkb@airmail.net](mailto: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.
> 
> ```
