Gaspard Bucher
9/12/2007 6:21:00 AM
This is fine to get the full list but you cannot get the 1200th item
without expanding a list.
This doesn't really look like a wheel to me:
def convert(v)
res = ''
while(v >= 0)
res = (?a + (v % 26)).chr + res
v = (v/26) - 1
end
res
end
2007/9/12, Morton Goldberg <m_goldberg@ameritech.net>:
> On Sep 11, 2007, at 8:04 PM, Rob wrote:
>
> > I have corrected the class code,
> >
> > # This version includes the enumerable module, gaining access
> > to .collect .inject .each_with_index et al.
> > class Generator
> > include Enumerable
> >
> > def initialize(start="a", limit=52)
> > @limit = limit
> > @start = start
> > end
> >
> > def each
> > # Dup here otherwise another call to .each later on starts
> > from where this left off
> > char = @start.dup @limit.times do
> > # And dup here otherwise the values returned will point
> > to the same location
> > # and .collect would return ["ba", "ba", "ba", ... "ba"]
> > or whatever the last
> > # character sequence was.
> > yield char.dup
> > char.succ!
> > end
> > end
> > end
> >
> > gen = Generator.new
> >
> > gen.each { |char| puts char }
> >
> > # comma separated string
> > puts gen.inject { |memo, s| "#{memo},#{s}" }
> >
> > # returns array of codes
> > puts gen.collect
>
> It seems to me that you are reinventing the wheel -- in this case
> 'wheel' being the built-in class Range. Why not do it like this?
>
> ('a'..'az').each { |chr| puts chr }
>
> ('a'..'az').to_a.join(', ') # => "a, b, c, d, e, f, g, h, i, j, k, l,
> m, n, o, p, q, r, s, t, u, v, w, x, y, z, aa, ab, ac, ad, ae, af, ag,
> ah, ai, aj, ak, al, am, an, ao, ap, aq, ar, as, at, au, av, aw, ax,
> ay, az"
>
> ('a'..'az').to_a # => ["a", "b", "c", "d", "e", "f", "g", "h", "i",
> "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
> "x", "y", "z", "aa", "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai",
> "aj", "ak", "al", "am", "an", "ao", "ap", "aq", "ar", "as", "at",
> "au", "av", "aw", "ax", "ay", "az"]
>
> Regards, Morton
>
>