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.
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.
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.
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”
Um, no, I don't think that fits the bill :o)
- Warren B.
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.
‘a-z’)
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.
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.
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.
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.
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
Am Samstag, 14. Jul 2007, 05:20:46 +0900 schrieb Mike M.:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.