草庐IT

hash_instance

全部标签

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 - #<Hash> 的未定义方法 `bytesize'

我想将博主标签存储和更新到GAE中的数据存储区。当我运行该代码时,出现此错误:javax.servlet.ServletContextlog:ApplicationError/base/data/home/apps/yet-another-problem/1.334886515480009498/WEB-INF/gems/gems/sinatra-0.9.2/lib/sinatra/base.rb:45:in`each':undefinedmethod`bytesize'for#(NoMethodError)代码classLabelsclassLabelDataincludeBumbl

ruby-on-rails - instance_eval 是如何工作的,为什么 DHH 讨厌它?

大约在hisRailsConfpresentation的19:00点,DavidHeinemeierHansson谈到了instance_eval的缺点:ForalongtimeIrantedandravedagainstinstance_eval,whichistheconceptofnotusingayieldedparameter(likedo|people|)andjuststraightdosomethingandthenevaluatewhat'sinthatblockwithinthescopeofwhereyoucamefrom(Idon'tevenknowifthat

ruby - 使用 class_eval 和 instance_eval 访问 Ruby 类变量

我有以下内容:classTest@@a=10defshow_a()puts"a:#{@@a}"endclass为什么以下工作:Test.instance_eval{show_b}b:40=>nil但是我不能直接访问@@b?Test.instance_eval{@@b}NameError:uninitializedclassvariable@@binObject同样,下面的工作t=Test.newt.instance_eval{show_a}a:10=>nil但以下失败t.instance_eval{@@a}NameError:uninitializedclassvariable@@ai

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

ruby - 为什么 Ruby String.hash 跨机器不一致?

今天在我们跨多台服务器部署的应用程序上遇到了这个问题。我正在散列一些字符串以存储在共享键/值存储中。String的.hash方法根据服务器返回不同的整数。任何想法为什么?请注意,我对为什么感兴趣;无法解决。例子:server1$ruby-vruby1.9.2p180(2011-02-18revision30909)[x86_64-linux]server1$irbirb(main):001:0>"test".hash=>4146582576695053125server2$ruby-vruby1.9.2p180(2011-02-18revision30909)[x86_64-linux

ruby - 有没有办法在 Test::Unit 中撤消 any_instance 的摩卡 stub

很像thisquestion,我也在使用RyanBates的nifty_scaffold。它具有使用Mocha的any_instance的理想方面。在Controller后面的模型对象中强制进入“无效”状态的方法。与我链接到的问题不同,我没有使用RSpec,而是使用Test::Unit。这意味着那里的两个以RSpec为中心的解决方案对我不起作用。是否有通用的(即:与Test::Unit一起使用)删除any_instancestub的方法?我认为它导致我的测试出现错误,我想验证这一点。 最佳答案 碰巧,Mocha0.10.0允许uns

ruby - Consistent String#hash 仅基于字符串的内容

目标:将服务器处理的每个URL映射到0、1、2或3,尽可能均匀分布。虽然documentation因为ruby​​的String#hash方法说它将“根据字符串的长度和内容返回一个散列”,这显然不是全部。给定字符串的哈希在解释器的调用中不一致:$irbruby-1.9.2-p180:001>"foo".hash=>360517580588231756ruby-1.9.2-p180:002>^D$irbruby-1.9.2-p180:001>"foo".hash=>-2716152678666510148这意味着特定字符串的散列值可能会因服务器而异。Rails在内部使用String#ha

Ruby Hash .keys 和 .values,可以安全地采用相同的顺序吗?

基本的irb测试表明RubyHash以匹配顺序返回.keys和.values。假设是这种情况是否安全? 最佳答案 是的。根据RubyDocsforHash,"哈希按照插入相应键的顺序枚举它们的值。"因此,如果以相同的方式创建哈希,您应该始终获得相同的哈希顺序。 关于RubyHash.keys和.values,可以安全地采用相同的顺序吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question