草庐IT

about_nil

全部标签

ruby - 导轨 3 : method delegations with nil relationships

给定以下(大大简化的)对象:classPlayer:player,:prefix=>:playerend我需要在多个View中显示玩家名称。但是Player可能为nil是完全有效的(并且是预期的)。我目前通过以下方法处理此问题:classCharacter我不喜欢这个有几个原因。有更好的方法吗? 最佳答案 如果您主要处理与View相关的代码,您可能会发现在delegate调用中将“or”与allow_nil:true结合使用感觉很自然:Name:您可能会考虑的另一个有趣的模式是nullobjectpattern;使用此模式,您可以使

ruby - Ruby 中 undefined variable 等于 nil 吗?

我得到错误作为undefinedvariable,我知道nil如果用作bool值则被评估为false:ifyputs"Something"end 最佳答案 undefinedvariable不等于nil。未定义的instance变量返回nil(同样,如果它未定义)。y引发异常@y返回nil 关于ruby-Ruby中undefinedvariable等于nil吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow

ruby-on-rails - 在调用方法之前检查 nil 对象的更好方法是什么?

我有这个方法调用,我必须使用...financial_document.assets.length但是financial_document.assets可能是nil。我可以用...financial_document.assets.nil??'0':financial_document.assets.length有没有重复性较低的方法? 最佳答案 DaveW.Smith的方向是正确的。检查一下:http://www.nach-vorne.de/2007/4/24/attr_accessor-on-steroids一个简单的解决方案如

ruby - gem 更新——系统返回 "no implicit conversion of nil into String"

运行gemupdate--system不断返回错误#gemupdate--systemUpdatingrubygems-updateERROR:Whileexecutinggem...(TypeError)noimplicitconversionofnilintoString如何解决?详细:http://pastebin.com/2uBYEMTi 最佳答案 这可能是由于不兼容的gem版本(也许是一个正在做Monkey补丁的gem?)。您可以尝试更新单个gem吗? 关于ruby-gem更新

ruby-on-rails - Geocoding API 的响应不是有效的 JSON。和 request.location = nil

ifRails.env.development?@current_location_geo=Geocoder.search(request.remote_ip).firstelse@current_location_geo=request.locationendif!@current_location_geo.nil?&&@current_location_geo.ip=="127.0.0.1"@departure_currency_code="AUD"@departure_currency_name=["AustralianDollar(AUD$)","AUD"]else@count

ruby - super(&nil) 在 ruby​​ 中做什么?

我正在阅读thesourcecodeforconcurrent-ruby,并遇到了这行ruby​​代码。definitialize(*args,&block)super(&nil)#有人可以向我解释一下它应该做什么吗? 最佳答案 您必须首先了解此处使用的&运算符。参见示例:#The&hereconvertsablockargumenttoaprocdefa(&blk)end#The&hereconvertstheproctoablocka(&Proc.new{true})在proc=>block的情况下,它也可以将一些对象变成pro

Ruby - 检查 if block_given 之间有什么区别?和!block.nil?

我有一个ruby​​方法需要检查是否有block传递给它。一位同事建议简单地检查block.nil?是否在性能上稍微快一些并且适用于命名block。这已经很烦人了,因为他正在使用命名block并使用block.call而不是yield调用它,后者已被证明是significantlyfaster,因为命名block在可读性方面更容易理解。版本1:defnamed_block&blockifblock.nil?puts"Noblock"elseblock.callendend版本2:defnamed_block&blockif!block_given?puts"Noblock"elsebl

ruby - NoMethodError(未定义方法 `[]' 为 nil :NilClass)

我有一个非常奇怪的错误实例:NoMethodError(undefinedmethod`[]'fornil:NilClass):app/controllers/main_controller.rb:150:in`blockinfind_data_label'app/controllers/main_controller.rb:149:in`each'app/controllers/main_controller.rb:149:in`find_data_label'app/controllers/main_controller.rb:125:in`data_string'app/cont

ruby-on-rails - 访问嵌套哈希时如何避免 nil 元素的 NoMethodError?

这个问题在这里已经有了答案:RubyStyle:Howtocheckwhetheranestedhashelementexists(16个答案)HowtoavoidNoMethodErrorformissingelementsinnestedhashes,withoutrepeatednilchecks?(16个答案)关闭7年前。如果我尝试访问不存在的哈希元素,我会收到NoMethodError:undefinedmethod'[]'fornil:NilClass。但是,我无法预测会出现哪些元素。@param_info={}@param_info["drug"]["name"]#=>N

ruby - 分配给数组并替换出现的 nil 值

您好!如下所示,当为数组赋值时,如何将nil替换为0?array=[1,2,3]array[10]=2array#=>[1,2,3,nil,nil,nil,nil,nil,nil,nil,2]如果在分配时不可能,我将如何以最好的方式完成?我想到了array.map{|e|电子零??0:e},但是……谢谢! 最佳答案 赋值后改变数组:array.map!{|x|x||0}请注意,这也会将false转换为0。如果你想在赋值时使用零,会有点乱:i=10a=[1,2,3]a+=([0]*(i-a.size))[1,2,3,0,0,0,0,0