Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将长固定数转换为数组Ruby_Ruby_Arrays_Fixnum - Fatal编程技术网

将长固定数转换为数组Ruby

将长固定数转换为数组Ruby,ruby,arrays,fixnum,Ruby,Arrays,Fixnum,ruby中是否有一种方法可以将类似fixnum的74239转换为类似[7,4,2,3,9]的数组?也许不是最优雅的解决方案: 74239.to_s.split('').map(&:to_i) 输出: [7, 4, 2, 3, 9] [7, 4, 2, 3, 9] 你不必为了这类事情而往返于串地: def digits(n) Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 } end ary = digits(7

ruby中是否有一种方法可以将类似fixnum的
74239
转换为类似
[7,4,2,3,9]
的数组?

也许不是最优雅的解决方案:

74239.to_s.split('').map(&:to_i)
输出:

[7, 4, 2, 3, 9]
[7, 4, 2, 3, 9]

你不必为了这类事情而往返于串地:

def digits(n)
  Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 }
end

ary = digits(74239)
# [7, 4, 2, 3, 9]
这确实假设
n
是正的,当然,如果需要,将
n=n.abs
放入混合液中可以解决这个问题。如果需要覆盖非正值,则:

def digits(n)
  return [0] if(n == 0)
  if(n < 0)
    neg = true
    n   = n.abs
  end
  a = Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 }
  a[0] *= -1 if(neg)
  a
end
def数字(n)
如果(n==0),则返回[0]
if(n<0)
负=真
n=n.abs
结束
a=Math.log10(n).floor.downto(0).map{| i |(n/10**i)%10}
a[0]*=-1如果(负)
A.
结束

可以使用divmod方法一次提取一个数字

def digits n
  n= n.abs
  [].tap do |result|
    while n > 0 
      n,digit = n.divmod 10
      result.unshift digit
    end
  end
end
快速基准测试表明,这比使用日志提前查找位数要快,而日志本身比基于字符串的方法要快

bmbm(5) do |x|
  x.report('string') {10000.times {digits_s(rand(1000000000))}}
  x.report('divmod') {10000.times {digits_divmod(rand(1000000000))}}
  x.report('log') {10000.times {digits(rand(1000000000))}}
end

#=>
             user     system      total        real
string   0.120000   0.000000   0.120000 (  0.126119)
divmod   0.030000   0.000000   0.030000 (  0.023148)
log      0.040000   0.000000   0.040000 (  0.045285)

您可以转换为字符串并使用chars方法:

74239.to_s.chars.map(&:to_i)
输出:

[7, 4, 2, 3, 9]
[7, 4, 2, 3, 9]

它比拆分更优雅。

您也可以使用
数组。新的
而不是
映射

n = 74239

s = Math.log10(n).to_i + 1 # Gets the size of n

Array.new(s) { d = n % 10; n = n / 10; d }.reverse 

在Ruby 2.4中,整数将有一个。

从Ruby 2.4开始,整数()有一个内置的
数字
方法,可以将它们提取到一个数字数组中:

74239.0位
=> [9, 3, 2, 4, 7]
如果您想保持数字的顺序,只需将

74239.digits.reverse
=> [7, 4, 2, 3, 9]

Docs:

.map(&:to_i)
让它更优雅一点吗?
-23.to_.split('').map(&:to_i)
?我在这里有点吹毛求疵,但是
74239.to_.chars
似乎比
74239.to_.split('')
更合适,尤其是当与
map(&:to_i)结合使用时
:)我认为
每个字符都更合适,因为它可以避免
字符创建的中间
数组
。@Drenmi,我想
.chars
目前在枚举器中,而不是数组。@RyanBigg:除非你忘了高中数学。mbds的答案,使用Ruby 2.4中引入的
digits
方法会使所有其他方法都过时。