草庐IT

any_variable

全部标签

javascript - 在 Ruby 和 JavaScript 中测试 undefined variable ?

在JavaScript中,有一种有用的方法可以测试从未在任何给定点定义的变量。例如,如果尚undefinedvariablebob,则以下代码片段将返回true:typeof(bob)=='undefined'如何在Ruby中完成相同的测试?编辑:我正在寻找一个本质上同样紧凑的测试。我使用异常等提出了一些笨拙的近似值,但它们不是很漂亮! 最佳答案 defined?(variable_name)irb(main):004:0>defined?(foo)=>nilirb(main):005:0>foo=1=>1irb(main):006

ruby-on-rails - 名称错误 : undefined local variable or method `logger'

当我运行“脚本/服务器”时,一切正常,但是当我运行我的单元测试(raketest:units)时,我得到了下面的错误,我不知道如何解决这个问题.错误NameError:undefinedlocalvariableormethod`logger'for#/Users/kamilski81/Sites/pe/vitality_mall/vendor/rails/actionpack/lib/action_controller/test_process.rb:471:in`method_missing'/Users/kamilski81/Sites/pe/vitality_mall/lib/

ruby - 有没有办法在 Test::Unit 中撤消 any_instance 的摩卡 stub

很像thisquestion,我也在使用RyanBates的nifty_scaffold。它具有使用Mocha的any_instance的理想方面。在Controller后面的模型对象中强制进入“无效”状态的方法。与我链接到的问题不同,我没有使用RSpec,而是使用Test::Unit。这意味着那里的两个以RSpec为中心的解决方案对我不起作用。是否有通用的(即:与Test::Unit一起使用)删除any_instancestub的方法?我认为它导致我的测试出现错误,我想验证这一点。 最佳答案 碰巧,Mocha0.10.0允许uns

ruby - @instance_variable 和 attr_accessor 的区别

我刚开始学习ruby​​,我看不出@instace_variable和使用attr_accessor声明的属性之间的区别。下面两个类有什么区别:classMyClass@variable1end和classMyClassattr_accessor:variable1end我在网上查了很多教程,每个人使用的表示法都不一样,这和ruby版本有什么关系吗?我还在StackOverflow中搜索了一些旧线程Whatisattr_accessorinRuby?What'stheDifferenceBetweenTheseTwoRubyClassInitializationDefinitions?

ruby-on-rails - 我得到 "found character that cannot start any token while scanning for the next token"

我已经在我的笔记本电脑上运行RubyonRails大约一个月了,但是当我想在这个实例中运行服务器时(它在几个小时前工作正常)我现在收到这条消息。请问如何让服务器再次运行?C:\Sites\LaunchPage>railssC:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/psych.rb:203:in`parse':():foundcharacterthatcannotstartanytokenwhilescanningforthenexttokenatline17column17(Psych::SyntaxError)fromC:/RailsIns

ruby - 线程安全 : Class Variables in Ruby

在Ruby中对类变量执行写入/读取操作不是线程安全的。对实例变量执行写入/读取似乎是线程安全的。也就是说,对类或元类对象的实例变量执行写入/读取是否线程安全?这三个(人为的)示例在线程安全方面有何区别?示例1:相互排斥classBestUser#(singletonclass)@@instance_lock=Mutex.new#Memoizeinstancedefself.instance@@instance_lock.synchronizedo@@instance||=bestendendend示例2:实例变量存储classBestUser#(singletonclass)#Memo

ruby-on-rails - RSpec any_instance 弃用 : how to fix it?

在我的Rails项目中,我使用rspec-mocks和any_instance但我想避免这个弃用消息:使用rspec-mocks的旧:should语法中的any_instance而不显式启用该语法已被弃用。使用新的:expect语法或明确启用:should。这是我的规范:describe(".create")doit'shouldreturnerrorwhen...'doUser.any_instance.stub(:save).and_return(false)post:create,user:{name:"foo",surname:"bar"},format::jsonexpect

ruby 模板 : How to pass variables into inlined ERB?

我有一个内联到Ruby代码中的ERB模板:require'erb'DATA={:a=>"HELLO",:b=>"WORLD",}template=ERB.newcurrentvalueis:EOFDATA.keys.eachdo|current|result=template.resultoutputFile=File.new(current.to_s,File::CREAT|File::TRUNC|File::RDWR)outputFile.write(result)outputFile.closeend我无法将变量“current”传递到模板中。错误是:(erb):1:undefi

ruby - "Could not find a valid gem in any repository"(rubygame 等)

一段时间以来我一直在尝试安装这个“rubygame”gem,但是每当我使用命令时geminstallrubygame会报错:ERROR:Couldnotfindavalidgem'rubygame'(>=0)inanyrepositoryERROR:Whileexecutinggem...(Gem::RemoteFetcher::FetchError)Errno::ETIMEDOUT:Connectiontimedout-connect(2)(http://rubygems.org/latest_specs.4.8.gz)我也尝试过其他gems但结果相似:ERROR:Couldnotf

ruby-on-rails - rails : Should partials be aware of instance variables?

例如,RyanBates的nifty_scaffolding就是这样做的编辑.html.erb'form'%>new.html.erb'form'%>_form.html.erb那种隐藏的状态让我觉得不舒服,所以我通常喜欢这样做编辑.html.erb'form',:locals=>{:object=>@my_object}%>_form.html.erb那么哪个更好:a)让部分访问实例变量或b)传递部分它需要的所有变量?最近我一直选择b),但我确实遇到了一些问题:some_action.html.erb'partial',:locals=>{:son=>a_son}%>_partial