我试图找到一种使用八进制编号引用数组索引的简洁方法.如果我正在寻找八进制13的数组索引,它应该返回值a[11].
这就是我想要实现它的目的,但它看起来并不优雅或高效:
a = [ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62 ]
v = 13
puts a[v.to_s.to_i(8)] # => 61
# OR
puts a[v.to_s.oct] # => 61
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?