jjzjj

variable-declaration

全部标签

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 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 - @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

ruby - 总新手 : Instance variables in ruby?

请原谅新手问题,但为什么@game_score总是零?#bowling.rbclassBowling@game_score=0defhit(pins)@game_score=@game_score+pinsenddefscore@game_scoreendend 最佳答案 让我们看一下代码,好吗?#bowling.rbclassBowling@game_score=0#(1)此时(1),我们仍在classBowling中。记住:类和其他对象一样只是对象。因此,此时您将0分配给类对象Bowling的实例变量@game_score。de

ruby-on-rails - Ruby on Rails : pretty print for variable. hash_set.inspect ...有没有办法在控制台中漂亮地打印 .inspect ?

我发现自己在我的功能测试中做了很多puts.inpsects以确保我知道数据是如何格式化的......但是当散列对象中的每个条目之后没有新行时散列很难读取.无论如何,也许是一个gem?,pretty-print哈希?所以它看起来像这样:{entry1=>{entrey1.1=>1,entry1.2=>3},entry2=>3}而不是:{entry1=>{entrey1.1=>1,entry1.2=>3},entry2=>3}?谢谢! 最佳答案 你可以为此使用awesome_printgem。https://github.com/mi

ruby - 为什么要在 Ruby 中避免使用 @@class_variables?

我知道有人说在Ruby中应该避免使用类变量(例如@@class_var),而应该在类范围:defMyClass@@foo='bar'#Shouldnotdothis.@foo='bar'#Shoulddothis.end为什么在Ruby中不赞成使用类变量? 最佳答案 类变量经常因为它们在继承方面有时令人困惑的行为而经常受到诽谤:classFoo@@foo=42defself.foo@@fooendendclassBar23Bar.foo#=>23如果你改用类实例变量,你会得到:classFoo@foo=42defself.foo@f

Ruby 类 : initialize self vs. @variable

有人可以解释在定义类时初始化“self”和使用@variables之间的区别吗?举个例子classChild所以在这种情况下,我不能用@stuff替换self.stuff吗?有什么不同?此外,super()只是意味着Parent初始化方法中的任何内容,Child应该直接继承它,对吗? 最佳答案 一般来说,不是,self.stuff=stuff和@stuff=stuff是不一样的。前者在对象上调用stuff=方法,后者直接设置实例变量。前者调用一个可能是公共(public)的方法(除非在类中特别声明为私有(private)),而后者总

ruby-on-rails - 如何检查是否存在值不为 "undefined local variable or method"的变量?

这是一个常见的模式:如果变量不存在,我会得到一个未定义的局部变量或方法错误。现有代码有ifvariable_name.present?但这并没有说明变量不存在。我如何检查变量的值并说明它根本不存在?我试过:if(defined?mmm)thenifmmm.present?thenputs"true"endend但Ruby仍然检查内部mmm.present?并在它不存在时抛出“nosuchvariable”。我确信对此有一个共同的模式/解决方案。 最佳答案 将present?更改为!=''并使用&&运算符,它仅在第一个表达式为真时才尝