Iterate through alphabet in Ruby until X

Issue

When using input `x`, I’m trying to iterate through the alphabet through to that point so, if I put in 44, I’ll iterate to 18 from this method.

I can see a lot of methods on SO for iteration a..z, a..zzz, etc, but less for iteration to position x and outputting the associated letters. Is there a ruby method for flipping an input letter to a number within a dynamic range?

``````def get_num(x)
pos = x%26
(1..pos).each do |c|
puts c
#outputs letter for position c
# end
end

get_num(44) # => Expected: 44%26 = 18; iterate 1 to 18 (pos) to get A..R list as output.
``````

Solution

Using the `#Integer.chr` method, `'a'..'z' == 97..122`, and `'A'..'Z' == 65..90` That means:

``````def get_num(x)
pos = x%26
(96+pos).chr
end

get_num(44)
#=>  "r"
``````

OR

``````def get_num(x)
pos = x%26
(64+pos).chr
end

get_num(44)
#=>  "R"
``````

``````def get_num(x)
pos = x%26
(1..pos).each do |c|
puts (c+64).chr
end
end

get_num(44)
#=>
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
``````