草庐IT

variable

全部标签

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 - 在 Ruby 中,为什么在启动 irb 之后出现 foo.nil?说未定义的错误,@foo.nil?给出 "true"和 @@wah.nil?又报错了?

在Ruby1.8.7和1.9.2中相同:$irbruby-1.8.7-p302>foo.nil?NameError:undefinedlocalvariableormethod`foo'for#from(irb):1ruby-1.8.7-p302>@bar.nil?=>trueruby-1.8.7-p302>@@wah.nil?NameError:uninitializedclassvariable@@wahinObjectfrom(irb):3为什么实例变量与局部变量和类变量的处理方式不同? 最佳答案 在Ruby中,大多数未初始化

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-on-rails - 从数据库中删除一条记录

在RubyonRails中,是@variable.delete还是@variable.destroy 最佳答案 @variable.destroy将调用所有回调(before_destroy等)并确保尊重关联。@variable.delete只是调用原始数据库查询来删除对象。通常,使用destroy会更安全,即使它更昂贵。 关于ruby-on-rails-从数据库中删除一条记录,我们在StackOverflow上找到一个类似的问题: https://stack

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 - 如何访问类变量?

classTestController我得到错误undefinedmethod'varible'forTestClass:Class在线@goodbay=TestClass.varible怎么了? 最佳答案 在Ruby中,读取和写入对象的@instance变量(和@@class变量)必须通过该对象的方法来完成。例如:classTestClass@@variable="var"defself.variable#Returnthevalueofthisvariable@@variableendendpTestClass.variable

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)),而后者总