Quick quesiton abt an array of characters

This seems like it’d be obvious but haven’t had any luck with it…

Let’s say I want to build an array of characters in order, i.e.

characters = [‘a’,‘b’,‘c’,‘d’,…]

If they were numbers I could do this:

r = []
(0…9).each { |x| r << x}

but how to do for characters?

Thanks

brez! !! wrote:

but how to do for characters?

Thanks

Same…

irb(main):001:0> (‘a’…‘z’).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”]

HTH
-Justin

brez! !! wrote:

but how to do for characters?

Thanks

irb(main):006:0> r = []
=> []
irb(main):007:0> (‘a’…‘z’).each {|d| r << d}
=> “a”…“z”
irb(main):008:0> r
=> [“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”]

or

irb(main):009:0> r = []
=> []
irb(main):010:0> c = ‘a’
=> “a”
irb(main):011:0> 26.times { r << c; c = c.succ}
=> 26
irb(main):012:0> r
=> [“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”]

very nice - thank you

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs