草庐IT

ruby - method_name(*) 是什么意思?

这个问题在这里已经有了答案:nakedasteriskasparameterinmethoddefinition:deff(*)(1个回答)关闭8年前。在轨道上code我遇到了以下方法定义definitialize(*)我明白deffoo(*a)的意思,但无法理解在*之后省略标识符名称的意义。您如何访问传递给此方法的任何参数?

ruby - 如何包含使用 FileUtils#sh 执行命令的管道?

我对Ruby的了解已经够多了,所以我主要通过API文档中的示例来编写一堆构建自动化脚本作为Rake任务。我正在使用如下代码编写一堆构建命令的脚本:results={}schemes.each{|scheme|command="xcodebuild","-scheme",schemeifoptions.configurationcommand.push"-configuration",options.configurationendcommand.push"archive"sh*commanddo|ok,res|results[scheme]=okend}这工作正常,但是会将大量很多输出

ruby /Chef : is there a way to refer to the resource 'name' and pass to a function?

请查看以下代码,使用Chef中的log资源。log'Hellothere'dolevel:infonotifies:run,"log_to_chat('Hellothere')"end当我将它传递给函数log_to_chat时,有没有办法引用资源name(在本例中:'Hellothere')。我想是这样的:log'Hellothere'dolevel:infonotifies:run,"log_to_chat(#{name})"end添加我对log_to_chat的尝试。尝试1:resource_name:log_to_chatproperty:message,kind_of:Stri

ruby-on-rails - Ruby 2.3.3 : Weird Tempfile. new([name, prefix]) basename converted to hash

为我们的Rails3.2.22.2应用程序测试到Ruby2.3.3的升级,并遇到一个奇怪的情况,我们将一个数组作为第一个参数传递给Tempfile.new,但它结束了作为哈希。我已经修补了tempfile.rb以输出传入的basename参数。在irbsession(非Rails)中,一切正常:>require'tempfile'true>Tempfile.new(['test','.csv'])["home",".csv"]#outputofbasenameargumentforTempfile.new=>#在railsconsolesession中:>Tempfile.new(['

ruby-on-rails - named_scope 中的变量字段名称?

在Rails模型中,我试图实现一个在start_date和end_date上过滤的named_scope。这很简单。但我将不得不在很多不同的领域多次这样做。这是自找麻烦吗?如果是这样,为什么(SQL注入(inject)?)还有另一种方法可以实现这一目标。named_scope:between,lambda{|start_date,end_date,field|{:conditions=>["#{field}>=?AND#{field}编辑:使用的解决方案我采用了Eggdrop的思路:@@valid_fields=%w(fieldsinhere)named_scope:between,l

ruby - 在 Ruby 中,self.user_name 与@user_name 不一样吗?

在Ruby中,不是像@foo这样的实例变量和@@bar这样的类变量吗?在一些代码中,我看到了一些self.user_name=@name甚至a+=1ifname!=user_name#thistime,withoutthe"self."#anditisthefirstlineofamethodso#itdoesn'tlooklikeitisalocalvariableself是为了什么?我认为它可能是一个访问器,但它不能只是user_name而不是self.user_name吗?而且我什至没有看到任何使其成为访问器的代码,例如attr_accessor,而且也不在基类中。

ruby - 如何将变量从 ruby​​ 传递到 sh 命令

我有一个运行的rake任务,有很多代码。最后,我需要使用sftp和ssh来做一些事情。目前我无法自动化它。有没有办法传递到标准输出?这似乎是一个简单的问题,但我无法在任何地方找到答案#somerubycode#morerubycodesh"sftpmyuser@hots"#thisopensthesftpconsolesh"putfile"#thisdoesn'tgetrununtilsftpisexitedsh"putanother_file"#neitherdoesthis#morerubycodesh"sshhost"#opensthesshconsolesh"some_acti

ruby-on-rails - rails 中的 action_name 方法有多可靠?

如何确保controller.action_name返回正确的信息?我依靠它在菜单栏中生成用户友好的消息。考虑以下场景。action_name方法在我使用时效果很好:redirect_toedit_profile_path#Igetaction_nameas"edit"inmyApplicationController但当我使用以下内容时,它不会产生我需要的信息:render:action=>"edit"#Theaction_nameshowsas"get"insteadof"edit"有没有一种方法可以使用渲染Action并仍然获得正确的action_name?谢谢,塔布雷兹----

ruby-on-rails - Rails 3 - NilClass :Class when i try to render `model_name' method 的未定义方法 'new'

我想在用户创建订单时将值保留在表单中(因此,如果用户在字段中有错误,那么用户将获得包含他/她已经打印的参数的页面)。所以我接下来做ControllerclassOrdersController",",:two_words_connector=>","endendend查看新订单:Order'screating{:class=>"form-horizontal"}do|f|%>"control-label"%>"span2",:placeholder=>@everpresent_field_placeholder%>"btnbtn-largebtn-success"%>但是当我输入错误的

ruby - gem 安装错误 : 'Please specify at least one gem name'

例如,当我运行“geminstallbundler”时,如果我运行的是ruby​​1.9.3版本,它工作正常,但是当运行ruby​​2.1.2时,它似乎没有看到“bundler”arg并给我ERROR:Whileexecutinggem...(Gem::CommandLineError)Pleasespecifyatleastonegemname(e.g.gembuildGEMNAME)我在两个运行rbenv的不同系统上看到了这一点。这是RBENV_DEBUG=1运行时输出的最后一部分:+[rbenv-exec:45]exportPATH=/home/ded/.rbenv/versio