草庐IT

local_variable_defined

全部标签

ruby - 为什么 Ruby 会在线上为最后一个 undefined variable 引发错误?

假设foo、bar和baz没有定义,行foobarbaz引发此错误:NameError(main:Object的未定义局部变量或方法“baz”)在Python、PHP和Javascript的REPL中,foo(bar(baz))中的第一个问题是未定义foo。为什么Ruby首先提示baz? 最佳答案 Ruby允许调用的第一个方法(baz)动态定义其他两个方法。在实际方法调用发生之前,它不会尝试将foo或bar解析为方法调用,并且它永远不会作为baz到达该方法调用首先导致错误。如果baz动态定义方法foo和bar,没有问题:defbaz

ruby-on-rails - rails : Where does new_*something*_path variable get set up?

我为“消息”创建了一个脚手架,并且new_message_path和edit_message_path(用于link_to's)都已设置,但现在我已经创建了app/views/messages/sent.html.erb,我想做类似的内容,但我不知道该怎么做。我明白了undefinedlocalvariableormethod`sent_message_path'for# 最佳答案 这些方法是在定义路由时自动创建的,对于RESTful路由,它们遵循可预测的约定。运行“rakeroutes”是查看生成的所有路由的有用方法。我建议您阅读

ruby-on-rails - rails : dynamically define class method based on parent class name within module/concern

我想根据包含此Mixin的类名在Mixin中动态生成一个类方法。这是我当前的代码:moduleMyModuleextendActiveSupport::Concern#defsome_methods#...#endmoduleClassMethods#HereiswhereI'mstuck...define_method"#{self.name.downcase}_status"do#dosomething...endendendclassMyClass但这给了我以下方法名称:MyClass.mymodule::classmethods_status在方法定义中获取基类名称是可行的(s

ruby-on-rails - Rails 4 关注 : Give class instance variables to model

考虑到Rails,我可以通过包含它们来通过模块提供我的模型类方法和实例方法。不过,我发现没有任何博客条目或线程提到如何在我的模型中包含变量。具体来说,我想给我的包含模型一个类实例变量@question,但我不知道将声明放在模块中的什么地方,所以它会被应用。如果模型本身声明了该变量,我还希望重写类实例变量。ActiveSupport::Concern模块真的关心变量吗?moduleContentAttributeextendActiveSupport::Concerndeffoop"hi"endmoduleClassMethods#@question="Iamagenericquesti

ruby "instance variable not initialized"警告

在用ruby​​编写一些“学习语言”代码时,作为linkedList实现的一部分,我遇到了这个警告:在“添加”方法中,如果头部不存在则创建它,即defadd(value)new_node=LinkedListNode.new(value)if!@head@head=new_nodeelseself.find{|node|node.next==nil}.next=new_nodeendend然后我收到警告.../linked_list.rb:13:warning:instancevariable@headnotinitialized如何摆脱这个警告?这样做的惯用方法是什么?

ruby-on-rails - 为什么我不能用重载方法在 define_method 中调用 super?

当我运行下面的代码时会引发错误:implicitargumentpassingofsuperfrommethoddefinedbydefine_method()isnotsupported.Specifyallargumentsexplicitly.(RuntimeError).我不确定是什么问题。classResultdeftotal(*scores)percentage_calculation(*scores)endprivatedefpercentage_calculation(*scores)puts"Calculationfor#{scores.inspect}"scores

ruby-on-rails - postgresql 数据库错误 : Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

当我运行rakedb:migrate或运行railss命令时,我得到同样的错误:Error:couldnotconnecttoserver:NosuchfileordirectoryIstheserverrunninglocallyandacceptingconnectionsonUnixdomainsocket"/var/run/postgresql/.s.PGSQL.5432"?当我尝试railss时,浏览器出现错误。这是我的database.ymldefault:&defaultadapter:postgresqlencoding:unicodepool:5development

ruby-on-rails - 不支持从 define_method() 定义的方法隐式传递 super 参数

在“AgileWebDevelopmentwithRails”(第三版)第537-541页中,“CustomFormBuilders”代码如下:classTaggedBuilder#Description##defself.create_tagged_field(method_name)define_method(method_name)do|label,*args|@template.content_tag("p",@template.content_tag("label",label.to_s.humanize,:for=>"#{@object_name}_#{label}")+"

ruby - @instance_variable 在 ruby​​ block 内不可用?

使用以下代码:defindex@q=""@q=params[:search][:q]ifparams[:search]q=@q@search=Sunspot.search(User)dokeywordsqend@users=@search.resultsend如果使用@q而不是q,搜索总是返回空查询("")的结果。为什么是这样?@q变量对do...endblock不可用吗? 最佳答案 这取决于block的调用方式。如果使用yield关键字或Proc#call方法调用它,那么您将能够在block中使用您的实例变量。如果使用Object

Ruby 动态类。如何修复 "warning: class variable access from toplevel"

我正在尝试编写一个程序,根据从文件中读取的配置动态定义ruby​​类。我知道我可以使用Class.new来做到这一点。这是一个示例程序:x=[1,2,3]Test=Class.newdo@@mylist=xdeffooputs@@mylistendendTest.new.foo当我运行它时,我得到以下输出(使用ruby​​1.9.3p0运行):c:/utils/test.rb:4:warning:classvariableaccessfromtoplevelc:/utils/test.rb:7:warning:classvariableaccessfromtoplevel123Does