Ruby 请问 Ruby 中 range 可以间隔生成数据吗?

fenginsc · May 06, 2021 · Last by ysllyfe replied at May 13, 2021 · 775 hits

比如我要生成 1 到 10 的数组

Array(1..10)

那么我要生成一个 1,3,5,7,9 的等差数列怎么办?

Reply to spike76

谢谢解答

print "ruby-china.org".unpack('H*')

"727562792d6368696e612e6f7267"

字符串按二位一组遍历怎么写?

Reply to tablecell

"727562792d6368696e612e6f7267".each_char.each_slice(2)

Reply to bromine0x23

老老实实遍历如何,这么写产生两组多余的数据,占内存

Reply to Awlter1

不就一点控制开销吗,内存使用能多多少?

"727562792d6368696e612e6f7267".scan(/../)

You need to Sign in before reply, if you don't have an account, please Sign up first.