I don't understand a little of the RUBY = [*? A ..? Z]
I found this code a few years ago. I understand what this code does, not how. Can anyone please explain what are doing here *
and ?
? I have never seen them use this before.
myarr = [*?a..?z] #generates an array of strings for each letter a to z
myarr = [*?a..?z] + [*?0..?9] # array of strings a-z and 0-9
source to share
?
is just a character literal, it had special meaning in ruby <1.9, but is now ?a
the same as when executed"a"
Then it ..
creates a Range and *
just expands that into an argument list and the [
]
pair turns that into an array.
Wish my google-fu was enough to get decent links or documentation explanations beyond that, but very hard to find.
Updated: ?a
is actually the same as "a"
not 'a'
as previously mentioned. To see this run (IRB tags left to help illustrate what's going on):
irb(main):001:0> print ?\t
=> nil
irb(main):002:0> print "\t"
=> nil
irb(main):003:0> print '\t'
\t=> nil
irb(main):004:0>
source to share