Ruby 将数组转换为 Fixnum [英] Ruby convert Array to Fixnum
本文介绍了Ruby 将数组转换为 Fixnum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在研究它时没有找到任何相关的东西.我基本上是在尝试将数组(例如 [5, 1, 7 ,8]
)转换为具有 5178
值的 fixnum.
I failed to find anything relevant when I was researching it. I am bascially trying to convert an array, for example [5, 1, 7 ,8]
to a fixnum which will have the value of 5178
.
推荐答案
[5, 1, 7 ,8].inject{|n, d| n * 10 + d}
# => 5178
对比
t = Time.now
100000.times do
[5, 1, 7 ,8].inject{|n, d| n * 10 + d}
end
Time.now - t # => 0.108719628
t = Time.now
100000.times do
[5, 1, 7, 8].join.to_i
end
Time.now - t # => 0.246565502
这篇关于Ruby 将数组转换为 Fixnum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文