草庐IT

ruby - Chef : Can a variable set within one ruby_block be used later in a recipe?

假设我有一个变量directory_list,我在名为get_directory_list的ruby​​_block中定义和设置了它。我可以稍后在我的Recipe中使用directory_list吗,或者编译/收敛过程会阻止这种情况吗?例子:ruby_block"get_file_list"doblockdotransferred_files=Dir['/some/dir/*']endendtransferred_files.eachdo|file|file"#{file}"dogroup"woohoo"user"woohoo"endend 最佳答案

ruby - Delayed_job : how to use handle_asynchronously to work with a function?

函数是:defcreateuser(name,pass,time)putsname,pass,timeend我试试:handle_asynchronously:createuser("a","b","c")得到一个错误:语法错误,意外'(',期待keyword_end谢谢。===编辑===日本的用户数据库和北京的网络服务器。所以我用这种方式来创建用户。defcreateuser(name,pass,time)Net::HTTP.get(URI.parse("http://www.example.net/builduser.php?hao=#{name}&mi=#{pass}&da=#{

ruby - Ruby 是否提供与 Perl 的 use strict 等效的功能?

很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visitthehelpcenter.关闭10年前。我的意思是,Ruby中没有my。我发现Perl中的usestrict可以提供非常好的防错字保护。

ruby-on-rails - rails : Using CanCan to define multiple roles depending on instances of single Model?

我目前一直在研究如何根据我们想要的每个条件来分离CanCan的角色。在我们的应用程序中,有很多类别(例如数学、英语、历史等),每个类别中都有很多类(class)。每个用户可以在每个类别中扮演许多不同的角色。例如,约翰可以是数学的“读者”,这意味着他可以阅读所有数学类(class)。约翰也可以是英语的“作家”,这意味着他可以阅读所有英语类(class),在类别英语中创建类(class),并仅编辑/删除他创建的英语类(class)。如果这些是John仅有的角色,他将无法在导航栏中看到类别历史记录,并且将被拒绝访问历史记录中的类(class)。这些是关系的建立方式:classUser在mod

ruby - 参数错误 : wrong number of arguments (1 for 0) when using afer_save

ArgumentError:wrongnumberofarguments(1for0)from/Users/Castillo/Desktop/gainer/app/models/status.rb:13:in`update_remaining_nutrients'from/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:507:in`blockincallback'from/usr/local/rvm/

ruby-on-rails - rails : Using groupdate & chartkick to create a cumulative user graph

我正在使用groupdate和chartkick来尝试显示一张图表,显示我们的用户群随时间的增长(和下降)。使用以下它在柱形图中工作正常,但在折线图中变得困惑:sum=0User.group_by_day(:created_at).count.map{|x,y|{x=>(sum+=y)}}.reduce({},:merge)谁能指出我正确的方向?或者是否有更好的方法来实现这一点? 最佳答案 因为在做累加和之前需要先排序试试这个:sum=0User.group_by_day(:created_at).count.to_a.sort{|

ruby - 如果没有 arg,则针对 stdin 运行的脚本;否则输入文件=ARGV[0]

这工作得很好-只是想知道是否有任何改进来缩短它?if(ARGV[0].nil?)theninput=$ 最佳答案 您可以完全删除前五行。来自镐$因此:print$Kernel.gets是$whiles=getsputssend 关于ruby-如果没有arg,则针对stdin运行的脚本;否则输入文件=ARGV[0],我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/2013428/

ruby-on-rails - rails/Prawn : how do I use rails helpers inside a Prawn class?

我正在尝试在prawn类中使用rails3.2助手,但rails抛出:undefinedmethod`number_with_precision'for#Prawn类classQuotePdfControllerdefshow@quote=current_user.company.quotes.where(:id=>params[:id]).firsthead:unauthorizedandreturnunless@quoterespond_with@quote,:layout=>!params[:_pjax]do|format|format.pdfdosend_dataQuotePd

ruby-on-rails - RubyOnRails : How do I use helper methods in Rails Console?

可能是我的设置有问题:irb(main):001:0>truncate("Onceuponatimeinaworldfarfaraway",:length=>17)NoMethodError:undefinedmethod`truncate'formain:Objectfrom(irb):1from/usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in`start'from/usr/lib64/ruby/gems/1.9.1/gems/railties-3.2.8/lib/ra

ruby-on-rails - ruby /rails : Use `separator: ' -'` instead

正在为我的项目编写测试,并在运行rspec时注意到这个终端警告DEPRECATIONWARNING:Passingtheseparatorargumentasapositionalparameterisdeprecatedandwillsoonberemoved.Use`separator:'-'`instead.(calledfromadd_linkat/myapp/app/models/post.rb:37)我在保存记录之前有一个Action,看起来像self.link=theme+'-'+Time.now.to_formatted_s(:number)我试图找到一些关于这个的信息