草庐IT

some_method_string

全部标签

Ruby 增量 (+=) 引发错误 undefined method '+' for nil :NilClass

以下代码导致了我的问题:classFoodefinitialize(n=0)@n=nendattr_accessor:ndefincn+=1endend调用Foo.new.inc引发NoMethodError:undefinedmethod'+'fornil:NilClass调用Foo.new.n返回0为什么Foo.new.inc会引发错误?我可以毫无问题地执行Foo.new.n+=1。 最佳答案 tldr;某种形式的self.n=x必须始终用于分配给setter。考虑n+=x扩展为n=n+x其中n被绑定(bind)为局部变量因为它

ruby - Aptana 3 ruby​​ 调试器 - DebugThread 循环中的异常 : undefined method `is_binary_data?'

我正在尝试在Aptana3中调试简单的ruby​​文件。classHelloWorlddefinitialize()enddefgreet()puts"helloworld"endendh=HelloWorld.newh.greet断点设置为h.greet在我开始调试后,调试器启动,但是当它尝试初始化ruby​​类时,调试器断开连接并显示消息FastDebugger(ruby-debug-ide0.4.9)listenson:54749ExceptioninDebugThreadloop:undefinedmethod`is_binary_data?'for"#":String当我将断

ruby - `-' :String 的未定义方法 "100"

这段Ruby代码:income="100"bills="52"putsincome-bills抛出一个错误:./to_f.rb:6:undefinedmethod`-'for"100":String(NoMethodError)在对字符串执行数学运算时,Ruby不会自动将字符串转换为数字吗? 最佳答案 Ruby是一个dynamically-typed,strictly-typed(或“强类型”)语言。Lua是另一种这样的语言。前者意味着变量可以包含任何类别的值。后者——你遇到的情况——意味着类型强制不会自动发生。将它们与动态类型和松

ruby - 导轨 3 : method delegations with nil relationships

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

ruby - 'string' 到 ['s' , 'st' , 'str' , 'stri' , 'strin' , 'string' ]

什么是最优雅的做法'string'=>['s','st','str','stri','strin','string']我一直在想一个类轮,但我不能完全做到。欢迎任何解决方案,谢谢。 最佳答案 这个怎么样?s='string'res=s.length.times.map{|len|s[0..len]}res#=>["s","st","str","stri","strin","string"] 关于ruby-'string'到['s','st','str','stri','strin','s

ruby-on-rails - 如何修复 `new' :String 的未定义方法 "Rack::Cors"

我是RubyonRails的新手,正在学习如何使用Angular,但是在我运行“geminstallrack-cors”之后,当我尝试启动Rails应用程序时,我保持收到此错误:C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/actionpack-5.1.1/lib/action_dispatch/middleware/stack.rb:35:in`build':undefinedmethod`new'for"Rack::Cors":String(NoMethodError)Didyoumean?nextfromC:/Ruby23-x64/lib/ruby

ruby - 无法在 Ruby 中将 StringIO 转换为 String (TypeError)

当我使用下面的代码时,我收到以下错误消息:无法将StringIO转换为String(TypeError)array_of_lines=[]Zip::ZipInputStream::open(open("URLforzippedfile","rb"))do|io|file=io.get_next_entryputs"Downloadingfile#{file}"array_of_lines=io.readlinesprint"Downloaded",array_of_lines.count,"elements.","\n"end有人可以帮助我吗?提前致谢。 最

ruby-on-rails - rails 错误 method_missing':Gem::Specification 的未定义方法 `this'

我遵循这个教程:https://guides.spreecommerce.com/developer/getting_started_tutorial.html#installing-image-magick当我写作时jonstark@jonstark-pc:~/rails_projects/optima1$spreeinstall--auto-accept我明白了:/home/jonstark/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/specification.rb:2158:in`method_missing':undefine

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 - 在 define_method 中调用 super 时没有父类(super class)方法

当我重写一个已经存在的方法时,为什么会出现以下错误talk:super:nosuperclassmethodtalk(NoMethodError)?如何修复此代码以调用super方法?这是我正在使用的示例代码classFoodeftalk(who,what,where)p"#{who}is#{what}at#{where}"endendFoo.new.talk("monster","jumping","home")classFoodefine_method(:talk)do|*params|super(*params)endendFoo.new.talk("monster","jump