Hey friends! What is the best way to convert a Numeric to a string encoded
with base 26? I want the string to contain just alpha characters, not
alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p', 'a-z')
and I'm wondering if there is a more concise way.
Hey friends! What is the best way to convert a Numeric to a string
encoded
with base 26? I want the string to contain just alpha characters, not
alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
'a-z')
and I'm wondering if there is a more concise way.
Hey friends! What is the best way to convert a Numeric to a string encoded
with base 26? I want the string to contain just alpha characters, not
alphanumeric characters. For now I'm using
num.to_s(26).tr('0-9a-p', 'a-z')
and I'm wondering if there is a more concise way.
Isn't that concise enough? It's one line, it's
comprehensible right away and it reflects higher logic.
So what do you want more?
Compare it to this:
class Fixnum
def to_a_z
n, r = abs, ""
begin
n, d = n.divmod 26
r.insert 0, (d+?a).chr
end while n > 0
r.insert 0, "-" if self < 0
r
end
end
Bertram
···
Am Samstag, 14. Jul 2007, 05:20:46 +0900 schrieb Mike Moore:
Hey friends! What is the best way to convert a Numeric to a string
encoded
with base 26? I want the string to contain just alpha characters, not
alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
'a-z')
and I'm wondering if there is a more concise way.
You're looking too far away.
int.to_s(26)
Regards
Stefan
OK, I should read before answering
Why are you using a-z instead of 0-9a-p?
Anyway, if you need that instead of what to_s(26) already does, then I
don't know a shorter way.
I think your solution is pretty concise already. You could however do something like this:
class Integer
C26 = (?a..?z).to_a.freeze
def to_b26
x = self
s = ""
until x == 0
m = x % 26
s << C26[m]
x = (x - m) / 26
end
s == "" ? "0" : s.reverse!
end
end
Or, a more generic solution:
class Integer
def to_base(chars)
raise ArgumentError if chars.empty?
return chars[0].chr if self == 0
x = self
s = ""
until x == 0
m = x % chars.size
s << chars[m]
x = (x - m) / chars.size
end
s.reverse!
end
end
Hey friends! What is the best way to convert a Numeric to a string encoded
with base 26? I want the string to contain just alpha characters, not
alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p', 'a-z')
and I'm wondering if there is a more concise way.
On Jul 13, 2:39 pm, Stefan Rusterholz <apei...@gmx.net> wrote:
Mike Moore wrote:
Hey friends! What is the best way to convert a Numeric to a string
encoded
with base 26? I want the string to contain just alpha characters, not
alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
'a-z')
and I'm wondering if there is a more concise way.
Anyway, if you need that instead of what to_s(26) already does, then I
don't know a shorter way.
Stefan Rusterholz wrote:
> Mike Moore wrote:
>> Hey friends! What is the best way to convert a Numeric to a string
>> encoded
>> with base 26? I want the string to contain just alpha characters, not
>> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
>> 'a-z')
>> and I'm wondering if there is a more concise way.
>>
>
> You're looking too far away.
> int.to_s(26)
>
> Regards
> Stefan
OK, I should read before answering
Why are you using a-z instead of 0-9a-p?
Because it is one extra step to switch to the number keys on the iPhone
keyboard.
Anyway, if you need that instead of what to_s(26) already does, then I
don't know a shorter way.
Yeah, I was looking for an offset value, something like
Numeric#to_s(base=10, offset=0)
Regards
···
On 7/13/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
On 7/13/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
Stefan Rusterholz wrote:
> Mike Moore wrote:
>> Hey friends! What is the best way to convert a Numeric to a string
>> encoded
>> with base 26? I want the string to contain just alpha characters, not
>> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
>> 'a-z')
>> and I'm wondering if there is a more concise way.
>>
>
> You're looking too far away.
> int.to_s(26)
>
> Regards
> Stefan
OK, I should read before answering
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck
If it ends up in base 36 then it's still going to get numbers in it, just
not for the first ten numbers!
To the original poster.. no, it looks like you're doing it the best way
IMHO. What you are trying to do is not standard (that is, base 26 is
0-9a-p), so a single 'tr' is nothng to sniff at to get your special variant Just abstract it away into your own method on Fixnum/Numeric if you want
to keep it lean in your other code.
Cheers,
Peter Cooper
···
On 7/13/07, Phrogz <phrogz@mac.com> wrote:
On Jul 13, 2:39 pm, Stefan Rusterholz <apei...@gmx.net> wrote:
> Mike Moore wrote:
>> Hey friends! What is the best way to convert a Numeric to a string
>> encoded
>> with base 26? I want the string to contain just alpha characters, not
>> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
>> 'a-z')
>> and I'm wondering if there is a more concise way.
> Anyway, if you need that instead of what to_s(26) already does, then I
> don't know a shorter way.
Hey friends! What is the best way to convert a
Numeric to a string encoded with base 26? I want
the string to contain just alpha characters, not
alphanumeric characters. For now I'm using
num.to_s(26).tr('0-9a-p','a-z')
and I'm wondering if there is a more concise way.
Anyway, if you need that instead of what to_s(26)
already does, then I don't know a shorter way.
How about (i+10).to_s(36)
You mean like:
irb(main):001:0> i = 42
=> 42
irb(main):002:0> (i+10).to_s(26)
=> "20"
On Jul 13, 2:39 pm, Stefan Rusterholz <apei...@gmx.net> wrote:
> Mike Moore wrote:
>> Hey friends! What is the best way to convert a Numeric to a string
>> encoded
>> with base 26? I want the string to contain just alpha characters, not
>> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
>> 'a-z')
>> and I'm wondering if there is a more concise way.
> Anyway, if you need that instead of what to_s(26) already does, then I
> don't know a shorter way.
How about (i+10).to_s(36)
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck
>
> > Mike Moore wrote:
> >> Hey friends! What is the best way to convert a Numeric to a string
> >> encoded
> >> with base 26? I want the string to contain just alpha characters,
not
> >> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
> >> 'a-z')
> >> and I'm wondering if there is a more concise way.
> > Anyway, if you need that instead of what to_s(26) already does, then I
> > don't know a shorter way.
>
> How about (i+10).to_s(36)
If it ends up in base 36 then it's still going to get numbers in it, just
not for the first ten numbers!
To the original poster.. no, it looks like you're doing it the best way
IMHO. What you are trying to do is not standard (that is, base 26 is
0-9a-p), so a single 'tr' is nothng to sniff at to get your special
variant Just abstract it away into your own method on Fixnum/Numeric if you
want
to keep it lean in your other code.
Thanks Peter. I didn't mean to sniff at a single 'tr', I was just hoping
for a better way. Right now I have my 'to_base26' and 'from_base26' methods
on Numeric and String respectively, but I think I might monkey-patch 'to_s'
and 'to_i' and add the base 26 logic when the 'base' argument is 26.
Cheers,
···
On 7/13/07, Peter Cooper <peter@peterc.org> wrote:
On 7/13/07, Phrogz <phrogz@mac.com> wrote:
> On Jul 13, 2:39 pm, Stefan Rusterholz <apei...@gmx.net> wrote:
Peter Cooper http://www.rubyinside.com/
You can do it whichever way you want, of course, but in terms of good coding
practices I'd advise not monkey patching to_s and to_i in this way as it
could break expected behavior (for libraries you use, etc). Monkey patching
is okay to add features, but /changing/ existing features can get messy.
Unless, of course, you add an optional param and detect that instead (e.g.
to_s(base, start='0') .. then use .to_s(26, 'a') for yourself), but by that
point it's possibly easier just to make your own methods.
Cheers,
Peter Cooper
···
On 7/13/07, Mike Moore <blowmage@gmail.com> wrote:
Thanks Peter. I didn't mean to sniff at a single 'tr', I was just hoping
for a better way. Right now I have my 'to_base26' and 'from_base26'
methods
on Numeric and String respectively, but I think I might monkey-patch
'to_s'
and 'to_i' and add the base 26 logic when the 'base' argument is 26.