Ruby --- custom #to_a automatic completion for excel-spreadsheet style alphanumeric coordinates

StackOverflow https://stackoverflow.com/questions/17098482

質問

In ruby I know you can do ("A".."D").to_a and get #=> ["A", "B", "C", "D"], and the same with numbers.

However, I'm trying to do something like this:

("19B".."23B").to_a
#=> ["19B", "20B", "21B", "22B", "23B"]

Ruby appears to be "getting it" to a degree, but not quite. This is what its outputting right now:

["19B", "19C", "19D", "19E", "19F", "19G", "19H", "19I", "19J", "19K", "19L", "19M", "19N", "19O", "19P", "19Q", "19R", "19S", "19T", "19U", "19V", "19W", "19X", "19Y", "19Z", "20A", "20B", "20C", "20D", "20E", "20F", "20G", "20H", "20I", "20J", "20K", "20L", "20M", "20N", "20O", "20P", "20Q", "20R", "20S", "20T", "20U", "20V", "20W", "20X", "20Y", "20Z", "21A", "21B", "21C", "21D", "21E", "21F", "21G", "21H", "21I", "21J", "21K", "21L", "21M", "21N", "21O", "21P", "21Q", "21R", "21S", "21T", "21U", "21V", "21W", "21X", "21Y", "21Z", "22A", "22B", "22C", "22D", "22E", "22F", "22G", "22H", "22I", "22J", "22K", "22L", "22M", "22N", "22O", "22P", "22Q", "22R", "22S", "22T", "22U", "22V", "22W", "22X", "22Y", "22Z", "23A", "23B"]

Not quite what I was looking for.

Is there anyway I could "lock" the letter-iteration like the $ operator does in excel?

役に立ちましたか?

解決

Just use the numeric part and append the letter afterwards:

(19..23).map { |i| "#{i}B" }
#=> ["19B", "20B", "21B", "22B", "23B"]

他のヒント

How about this:

cell1 = '19B'
cell2 = '23B'
(cell1[0..-2]..cell2[0..-2]).to_a.map {|o| o+cell1[-1]}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top