草庐IT

method_idx

全部标签

ruby - define_method 是否覆盖与其参数同名的方法?

我遇到了以下代码:classMethodLoggerdeflog_method((klass,method_name)klass.class_evaldoalias_method"#{method_name}_original"method_namedefine_methodmethod_namedoputs"#{Time.now}:Called#{method_name}"send"#{method_name}_original"endendendendclassTweetdefsay_hiputs"Hi"endendlogger=MethodLogger.newlogger.log

ruby-on-rails - 为什么我在 Rails 模型回调中收到此 "undefined method"错误,将方法作为符号传递?

我有一个名为“Post”的类,它应该在更改或尚未转换时将其Markdown内容转换为HTML。我正在尝试将before_save回调与if:一起使用参数,但我在传递给if的任何内容时都会收到此错误当我尝试运行测试时:Testingstartedat1:55...rakeaborted!undefinedmethod'markdown_changed_or_html_nil?'for#C:/Users/user/Documents/GitHub/jw/app/models/post.rb:7:inC:/Users/user/Documents/GitHub/jw/app/models/p

ruby : return from calling method

我希望能够在仍在被调用方法内的同时从调用方法返回。示例:defcalling_method#stuffcalled_method#morestuffenddefcalled_method#stuffreturn_from_calleriffoo#有没有一种简单的方法可以做到这一点?我目前使用的“肮脏”方式是这样的:defcalling_method#stuffcalled_methodandreturn#morestuffenddefcalled_method#stuffreturnfalseiffoo#morestuffend但这并不完全令人满意,因为我必须在调用方法中执行并返回。

ruby - 在 ruby​​ 脚本中访问 self.method 中的变量

我对ruby很陌生。我在获取self.method中的变量值时遇到问题我的ruby​​类/脚本如下所示:classA1definitialize@myUtil=MyUtil.newlog@test_hash=@myUtil.loadPropertiesFile(File.dirname(__FILE__)+'/my-tests.properties')enddefself.testCasestests={}@test_hash.eachdo|k,v|puts"#{k}"endtestsenddeflaunchtest,testbed,h={}@test_hash.eachdo|k,v|

ruby - "undefined method ' 变成 ' for nil:NilClass"不应该的地方

我今天在测试一个片段unlessresource.nil?resource=resource.becomes(Accounts::Admin)end这会引发错误undefinedmethod`becomes'fornil:NilClass如果我这样做unlessresource.nil?a=resource.becomes(Accounts::Admin)resource=aend一切顺利。如果先执行=运算符右边的部分有什么区别?编辑:发生了一些令人讨厌的事情,正在执行iffalse下的最后一行,但从未打印“ALOHA”。它打印AAAAUserid:nil,nome:nil,ender

ruby - 为什么Ruby中 "protected_class_method"类不存在实例方法 "Module"?

为什么Ruby中的“Module”类不存在实例方法“protected_class_method”,而“Module”类却存在“public_class_method”和“private_class_method”实例方法?这不遵循为“模块”类定义的“私有(private)”、“protected”和“公共(public)”实例方法的模式。 最佳答案 对我来说,protected方法只有作为实例方法才有意义。protected方法可以被同一类的其他实例调用。classStudentdefinitialize(age)@age=age

ruby-on-rails - 要扩展 Rails 的 `link_to` ,我应该使用 `alias_method_chain` 还是混合 + 继承?

我正在使用twitterbootstrap创建一个应用程序.我正在使用FontAwesome将图标添加到各个地方,通常是链接。到目前为止,我一直在使用全局助手。这是简化版本:#app/helpers/link_to_with_icon.rbdeflink_to_with_icon(text,path,options={})options=options.cloneicon=options.delete(:icon)text="#{text}"ificon.present?link_to(text,path,options)end这行得通,但每次我需要将图标添加到新链接时,我都必须记住将

ruby - 在 define_method 中使用局部变量

我想了解define_method的工作原理以及如何正确使用定义block之外的变量。这是我的代码:classTestdefself.pluginforiin1..2define_method("test#{i}".to_sym)dopiendendendpluginendob=Test.newob.test1#=>2(Iwouldexpect1)ob.test2#=>2(Iwouldexpect2)在test1和test2方法中,i的值似乎在定义时没有被替换,而是直接在调用方法时的位置。所以我们只能看到i的最新值,即2。但是Ruby从哪里获取这个值呢?有没有办法让test#{i}打印

ruby-on-rails - rails : why am I having trouble accessing this member variable in a controller method?

我在一个简单的Controller方法中发现了一个非常奇怪的问题。要么我遗漏了一些基本的东西,要么我遇到了一个错误。我打赌是前者。我有一个带有ThingController的Thing模型。Thing有两个变量,name和display,都是字符串。ThingController(下面的代码)有一个方法toggle_display,它在“on”之间切换display的内容>和“关闭”。问题是当我调用这个Action时,Rails找到了正确的Thing,但是@thing.display是nil。当我检查数据库时,“显示”列中的值是正确的。奇怪的是,当我取消注释下面代码中的第三行时(即访问

ruby-on-rails - rails : Undefined method 'to_sym'

我在部署的Rails2.3.5应用程序中遇到以下错误:NoMethodError(undefinedmethod`to_sym'fornil:NilClass):我使用Sqlite的应用程序的本地测试安装没有出现错误,但我部署的运行Mysql的应用程序出现了错误。两者之间唯一的区别是我在我的本地机器上运行Ruby1.8.7而在我的部署服务器上运行1.8.6。我已经包含了User.rb中的代码和下面的错误日志。我按照声明式授权和嵌入式授权Railscasts进行设置。编辑:这是application_controller的代码,我在其中使用before_filter设置了current_