草庐IT

environment-variables

全部标签

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

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

ruby - 获取名称错误 : `format' is not allowed as an instance variable name when testing instance variable with rspec

我有以下测试:let(:client){Descat::Client.new}describe'poblacio'doit'shouldsetformatcorrectly'doclient.poblacio('v1','json','dades')expect(client.instance_variable_get(:format)).toeq('json')endend我有以下正在测试的代码:moduleDescatclassClientBASE_URL='http://api.idescat.cat/'definitialize(attributes={})attributes

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 - @instance_variable 和 attr_accessor 的区别

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

ruby - 在 Ruby 中导出环境变量

如何将Ruby脚本中的环境变量导出到父shell?例如,实现readBash内置函数的简单实现:#!/usr/bin/rubyvarname=ARGV[0]ENV[varname]=STDIN.gets#buthavevarnameexportedtotheparentprocess 最佳答案 您不能将环境变量导出到运行ruby​​脚本的shell,但是您可以编写一个ruby​​脚本来创建一个源代码bash文件。例如%echoset_var.rb#!/usr/bin/envrubyvarname=ARGV[0]puts"#{varn

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 - Ruby 脚本可以告诉它在哪个目录中吗?

受“GettingthesourcedirectoryofaBashscriptfromwithin”的启发,Ruby的方法是什么? 最佳答案 对于较新版本的Ruby,请尝试:__dir__对于旧版本的Ruby(File.dirname(__FILE__)-相对路径;或File.expand_path(File.dirname(__FILE__))-绝对路径。注意:即使在调用Dir.chdir之后,使用__dir__也会返回脚本路径;而使用旧语法可能不会返回脚本的路径。 关于ruby-R

ruby-on-rails - 设置 'EDITOR' 或 'BUNDLER_EDITOR' 环境变量

我试图在SublimeText中打开一个gem,但没有成功。我安装了bundler。这是正在发生的事情。在命令行中:$bundleopenDeviseToopenabundledgem,set$EDITORor$BUNDLER_EDITOR当我去Bundlerwebsite它说,Openthesourcedirectoryofthegivenbundledgem:$bundleopenGEMThisopensthesourcedirectoryoftheprovidedGEMinyoureditor.ForthistoworktheEDITORorBUNDLER_EDITORenvir

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