jjzjj

instance-variables

全部标签

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-on-rails - Capistrano::Configuration:Class 的未定义方法 `instance'

我正在尝试在Rails应用程序中首次启动并运行Capistrano。我有一台运行Ubuntu12.04、nginx、unicorn和rails的linux服务器,但是,我似乎遇到了一些问题。我还使用RVM使用Capistrano3.0.0、rails3.2.14、bundler1.4.0和ruby​​1.9.3p448。我只设置了一个生产阶段,此时我只关心Capistrano与我的服务器通信并从github推送我的代码(目前还没有迁移和捆绑等)。当我使用下面的设置尝试命令capproductiondeploy:check或capproductiondeploy:setup(这似乎已被弃

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 - 助手设计 : could not find the `Warden::Proxy` instance on request environment

我尝试将Devise用于我的Rails应用程序。我可以注册并登录,但是当我转到其他页面“构建”时,出现以下错误:Devise::MissingWardeninHome#showDevisecouldnotfindtheWarden::Proxyinstanceonyourrequestenvironment.MakesurethatyourapplicationisloadingDeviseandWardenasexpectedandthattheWarden::Managermiddlewareispresentinyourmiddlewarestack.Ifyouareseeing

Ruby block 、procs 和 instance_eval

我最近尝试做类似的事情:a="somestring"b=Proc.new{upcase}a.instance_evalb这给出了错误:TypeError:can'tconvertProcintoString但这行得通:defb(&block)"somestring".instance_eval&blockendb{upcase}进一步了解此方法:defb(&block)"somestring".instance_evalblockend产生相同的ProctoString错误。所以...我对方block的理解是它们只是过程。但是显然,使用这个&符号有一些特别之处......有人能给我解释

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 - 声明实例变量遍历哈希!

我想做以下事情:我想声明一个遍历字典的类的实例变量。假设我有这个散列hash={"key1"=>"value1","key2"=>"value2","key3"=>"value3"}并且我想将每个键都作为类的实例变量。我想知道我是否可以声明迭代该散列的变量。像这样:classMyClassdefinitialize()hash={"key1"=>"value1","key2"=>"value2","key3"=>"value3"}hash.eachdo|k,v|@k=vendendend我知道这行不通!我只是放了这段代码,看看你是否能更清楚地理解我想要什么。谢谢!