From: Jani Monoses Date: 2004-09-23T20:00:42+09:00 Subject: Re: String#pad ? Martin DeMello wrote: > Jani Monoses wrote: > >>Hello >> >>is there a nicer way of padding a string to a power of two length than for example >> >>data = data.ljust((data.length+3) & ~3) >> >>this looks too much like C :) > > > And doesn't even work :) It does work the power of two is 4.That's why I said for example :) Ok my question meant a generic power of two but pasted the line from an actual program, my bad. > class Fixnum > def lg > self.to_s(2).length - 1 > end > end > > class String > def ljust_pow2 > self.ljust(2 ** (self.length.lg + 1)) > end > end > > p "hello world".ljust_pow2 This is even more work that the line above :( Jani