On 8/23/05, Philippe Creytens
<pcreytens-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
wrote:> Am I correct in understanding that Ruby has no
> equivalent function to the php str_pad_left function?
> I want to convert a string from say "1" to "0001" and
> pad for x char with zeroes.
I generally use a combination of String#rjust and #gsub, i.e:
my_string.rjust( 4 ).gsub( '' '', ''0'' )
If you use it regularly, you could always override #rjust to remove
the need for the #gsub:
class String
alias_method :space_padded_rjust, :rjust
def rjust( length, char = '' '' )
space_padded_rjust( length ).gsub( '' '', char )
end
end
--
Regards,
John Wilger
-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don''t know," Alice answered.
"Then," said the cat, "it doesn''t matter."
- Lewis Carrol, Alice in Wonderland