草庐IT

original_array

全部标签

arrays - 获取数组的每n个元素

如何使用Ruby1.9+选择数组的每个n个元素(例如六个)?示例:a=[0,1,2,3,4,...,33]#solution#=>[[0,1,2,3,4,5],[6,7,8,9,10,11],...] 最佳答案 Enumerable#each_sliceIteratesthegivenblockforeachsliceofelements.Ifnoblockisgiven,returnsanenumerator.e.g.:(1..10).each_slice(3){|a|pa}#outputsbelow[1,2,3][4,5,6][

ruby-on-rails - 文字和构造函数之间的区别? ([] 与 Array.new 和 {} 与 Hash.new)

我很想知道[]和Array.new以及{}和Hash.new之间的更多区别我对它进行了相同的基准测试,似乎简写是赢家require'benchmark'many=500000Benchmark.bmdo|b|b.report("[]\t"){many.times{[].object_id}}b.report("Array.new\t"){many.times{Array.new.object_id}}b.report("{}\t"){many.times{{}.object_id}}b.report("Hash.new\t"){many.times{Hash.new.object_id

Ruby - Array.join 与字符串连接(效率)

我记得有一次因为在Python中连接字符串而受到责骂。有人告诉我,在Python中创建一个字符串列表并稍后加入它们会更有效。我将这种做法应用到JavaScript和Ruby中,尽管我不确定这在后者中是否有同样的好处。谁能告诉我在Ruby编程语言中加入字符串数组并调用:join或根据需要连接字符串是否更有效(在资源和执行方面)?谢谢。 最佳答案 自己用Benchmark试试类。require"benchmark"n=1000000Benchmark.bmbmdo|x|x.report("concatenation")dofoo=""n

ruby 1.9 : Convert byte array to string with multibyte UTF-8 characters

我正试图在Ruby中找到一种方法来获取UTF-8字节数组并将其转换回字符串。在irb(Ruby1.9.2预览版3)中,我可以从UTF-8字符串创建正确的字节数组:ruby-1.9.2-preview3>'Café'.bytes.to_a=>[67,97,102,195,169]但是,我找不到从字节返回数组的方法。我尝试将Array.pack与U*选项一起使用,但这不适用于多字节字符。ruby-1.9.2-preview3>[67,97,102,195,169].pack('U*')=>"Café"有没有人知道如何将包含多字节字符的UTF-8字节数组转换回字符串?谢谢。

Ruby Array concat 与 + 速度?

我对Ruby的数组concat()与+操作进行了小型性能测试,concat()速度太快了。但是我不清楚为什么concat()这么快?有人可以帮忙吗?这是我使用的代码:t=Time.nowar=[]foriin1..10000ar=ar+[4,5]endputs"Timefor+"+(Time.now-t).to_st=Time.nowar=[]foriin1..10000ar.concat([4,5])endputs"Timeforconcat"+(Time.now-t).to_s 最佳答案 根据Rubydocs,不同之处在于:数组

ruby-on-rails - rails : Finding max of array that may contain nil

给定:shipping_costs={key1:45,key2:99,key3:nil,key4:24}假设nil=0,获取这些键的最大值的最简洁方法是什么?如果我在Rails控制台中直接运行shipping_costs.values.max,我会得到:ArgumentError:comparisonofFixnumwithnilfailed在运行max之前将这些nils变成零的最干净的方法? 最佳答案 如果你想让它非常简洁,你可以使用shipping_costs.values.compact.maxcompact方法从数组中删除所

ruby - 为什么 array.slice 对 (length, n) 的行为不同

如果我有一个数组a:a[a.length]返回nil。好。a[a.length,x]返回[]。好。a[a.length+x,y]返回nil。与2不一致。虽然此行为是documented,看起来很奇怪。谁能解释一下这种设计背后的原因? 最佳答案 考虑一下a=[0,1,2,3]#=>[0,1,2,3]a[0,10]#=>[0,1,2,3]a[1,10]#=>[1,2,3]a[2,10]#=>[2,3]a[3,10]#=>[3]a[4,10]#=>[]a[5,10]#=>nil所以a[4,10]是3之间的切片和数组的末尾[]哪里a[4]和

ruby 使用 array.map(& :methodname) for hash key strings rather than methodname 中的 "&:methodname"快捷方式

大多数ruby​​开发人员都知道如何通过执行以下操作来节省几次击键:array.map(&:methodname)而不是array.map{|x|x.methodname}有什么方法可以应用类似的&:methodname快捷方式来调用哈希数组上的“方法”(通过键调用的值)?在我的例子中,它的JSONapi结果以60个批处理作为源自JSON的散列数组返回。我试着这样做:array.map(&:"keyname")但没有成功,抛出一个NoMethodError并说Hash没有'keyname'方法,我想这是合理的。我想知道是否有一些Elixir可以模拟这个&:...ruby专家已经制定出的

ruby-on-rails - rails : Remove element from array of hashes

我有以下数组:array=[{"email"=>"test@test.com","name"=>"Test"},{"email"=>"testA@test.com","name"=>"TestA"},{"name"=>"TestB","email"=>"testB@test.com"},{"email"=>"testC@test.com","name"=>"TestC"},{"name"=>"TestD","email"=>"testD@test.com"},{"email"=>"testE@test.com"},{"name"=>"TestF","email"=>"testF@tes

arrays - Array#push 导致大数组出现 "stack level too deep"错误

我做了两个数组,每个数组有100万个项目:a1=1_000_000.times.to_aa2=a1.clone我试图将a2插入a1:a1.push*a2这将返回SystemStackError:stackleveltoodeep。但是,当我尝试使用concat时,我没有收到错误消息:a1.concata2a1.length#=>2_000_000我也没有得到splat运算符的错误:a3=[*a1,*a2]a3.length#=>2_000_000为什么会这样?我查看了Array#push的文档,它是用C语言编写的。我怀疑它可能在幕后进行一些递归,这就是它导致大型数组出现此错误的原因。这