草庐IT

create_array

全部标签

ruby - 运行时间 Array#unshift 与 Array#shift

我预计Array#shift和Array#unshift的运行时间都是Θ(n)。原因是机器需要遍历每个数组成员并将其分配给左侧或右侧的键。在Array#unshift的情况下,假设只有一个值作为参数传入并且有很多数组成员,我假设array[0]对运行时间没有显着影响。换句话说,当数组成员的数量很高而传递给Array#unshift的变量数量很少时,我期望Array#shift和Array#unshift以获得相同的运行时间。在Ruby2.1.2上运行基准测试时,这些假设不成立。为什么?代码:require'benchmark'GC.disablenumber_of_elements=2

ruby - 为什么 Array 不覆盖 Ruby 中的三重等号方法?

我刚刚注意到Array没有覆盖三重等号方法===,这有时被称为大小写相等方法。x=2casexwhen[1,2,3]then"match"else"nomatch"end#=>"nomatch"而范围运算符则:x=2casexwhen1..3then"match"else"nomatch"end#=>"match"但是,您可以为数组做一个变通办法:x=2casexwhen*[1,2,3]then"match"else"nomatch"end#=>"match"知道为什么会这样吗?是不是因为x更可能是一个实际的数组而不是一个范围,并且数组覆盖===意味着普通的相等性不会匹配?#Thisi

arrays - 数组元素赋值的奇怪行为

今天我遇到了数组元素赋值的一些奇怪行为:arr=["a","b"]arr2=[1,2]arr.unshift(arr2)#=[[1,2],"a","b"]arr.push(arr2)#=>["a","b",[1,2]]但是,这是有道理的:arr[0,0]=arr2#=>[1,2,"a","b"]我知道在[0,0]中,第一个零是index,第二个是该数组中从index开始的元素数。在我看来它应该与unshift相同,但事实并非如此。谁能解释一下这种行为? 最佳答案 如果我们diveintotherubysourcecode,我们会找到

ruby 雾 gem : how to create sub-directories?

我有connection=Fog::Storage.new(fog_config)bucket=connection.directories.get(bucket_name)有没有办法(已记录、未记录、变通)让我在此存储桶内创建目录?像这样的东西:sub_dir_for_user_1=bucket.create_sub_dir('/user_1_files')sub_dir_for_user_2=bucket.create_sub_dir('/user_2_files') 最佳答案 在S3中,带有尾部斜杠的零字节文件将创建一个伪目录

ruby - 为什么在:save callback hook not getting called from FactoryGirl. create()之前?

这个简单的示例使用DataMapper的before:save回调(又名Hook)来增加callback_count。callback_count初始化为0,回调应该设置为1。当通过以下方式创建TestObject时调用此回调:TestObject.create()但是当FactoryGirl通过以下方式创建时会跳过回调:FactoryGirl.create(:test_object)知道为什么吗?[注意:我正在运行ruby​​1.9.3,factory_girl4.2.0,data_mapper1.2.0]详细信息如下...DataMapper模型#file:models/test_

ruby-on-rails - ruby/rails array 两个索引之间的所有元素

我有一个这样的数组:[7,8,9,10,11,12,1,2,3,4,5,6]返回数组中从位置6到位置0的每个项目的最简单方法是什么,结果数组如下所示:[1,2,3,4,5,6,7]数组中的这个位置可以是动态的,例如传入4和9应该返回[11,12,1,2,3,4]我想知道是否有一种方法可以在Railsapi中实现这一点。提前致谢编辑假设没有负数,那么array[2..-2]将不起作用。Array#splice几乎适用于此,但如果第二个位置小于第一个,则返回nil。 最佳答案 deffooa,min,maxa.rotate(min).f

arrays - Ruby 中的并行分配性能

设置一个临时变量来交换数组中的两个元素似乎比使用并行赋值更有效。谁能帮忙解释下?require"benchmark"Benchmark.bmdo|b|b.reportdo40000000.times{array[1],array[2]=array[2],array[1]}endendBenchmark.bmdo|b|b.reportdo40000000.timesdot=array[1]array[1]=array[2]array[2]=tendendend结果:usersystemtotalreal4.4700000.0200004.490000(4.510368)usersyste

arrays - 在二维数组中搜索零并创建相应的行和列 0

这是我的代码,可以运行,但它太大了。我想重构它。req_row=-1req_col=-1a.each_with_indexdo|row,index|row.each_with_indexdo|col,i|ifcol==0req_row=indexreq_col=ibreakendendendifreq_col>-1andreq_row>-1a.each_with_indexdo|row,index|row.each_with_indexdo|col,i|print(req_row==indexori==req_col)?0:colprint""endputs"\r"endend输入:二

ruby-on-rails - 旧的 Ruby 错误在我的 Ruby on Rails 应用程序中反复出现,与 Class.create 和 delayed_job 相关

这个错误已经有好几个月了,在这里:http://www.ruby-forum.com/topic/1094002其中显示代码更改的两个链接:https://github.com/godfat/ruby/commit/f4e0e8f781b05c767ad2472a43a4ed0727a75708https://github.com/godfat/ruby/commit/c7a6cf975d88828c2ed27d253f41c480f9b66ad6我有Ruby1.9.2和rvm。我会把这些更改粘贴到适当的文件中,但我不知道如何粘贴。这在几天前就奏效了。我不能像这样执行RubyonRai

ruby-on-rails - 之前的工厂女孩​​(:create) callback not creating associations

我有一个FactoryGirl工厂,它创建一个Order但before(:create)回调不会创建关联的工厂对象:父类classOrder子类classOrderLine工厂Factory:orderdo...ignoredonumber_or_order_lines1endbefore(:create)do|order,evaluator|FactoryGirl.create_list:order_line,evaluator.number_or_order_lines,order:orderendendFactory:order_linedoassociation:userass