Project

General

Profile

Actions

Feature #5552

closed

Array#ljust, Array#rjust

Added by sawa (Tsuyoshi Sawada) over 12 years ago. Updated over 11 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-dev:44778]

Description

I propose the following methods Array#ljust(length [, pad]), Array#rjust(length [, pad]), whose behaviour is analogous to String#ljust(length [, padstr]), String#rjust(length [, padstr]).

[:a, :b, :c].ljust(2) #=> [:a, :b, :c]
[:a, :b, :c].ljust(5) #=> [:a, :b, :c, nil, nil]
[:a, :b, :c].ljust(5, :x) #=> [:a, :b, :c, :x, :x]

Updated by sawa (Tsuyoshi Sawada) over 12 years ago

And also Array#center analogous to String#center as well.

Updated by matz (Yukihiro Matsumoto) over 12 years ago

  • Status changed from Open to Feedback

なぜそれが必要なのか提案に含めてください。Stringにあるから、では不十分です。
私にはArray#ljustなどがあって嬉しい局面が想像できません。

Updated by trans (Thomas Sawyer) over 12 years ago

If you know pad length is greater than the array length then:

[:a,:b,:c].insert(5, nil)

Not sure about "rjust" though. I suppose there is always:

a.unshift(nil) until a.size == 5

Though that seems fairly inefficient.

Updated by yhara (Yutaka HARA) over 11 years ago

  • Status changed from Feedback to Rejected

Hi,

I'm closing this ticket because of no feedback.
Please reopen if you still need these methods.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0