草庐IT

data-access-app-block

全部标签

ruby - 我可以在传递给方法的 block 上强制执行元数吗?

有什么方法可以“开启”使用Proc.new或Kernel.proc实例化的Proc的严格元数强制执行,使其表现得像Proc用lambda实例化?我的initialize方法采用block&action并将其分配给实例变量。我希望action严格执行arity,因此当我稍后对其应用参数时,它会引发一个ArgumentError,我可以挽救它并引发一个更有意义的异常。基本上:classCommandattr_reader:name,:actiondefinitialize(name,&action)@name=name@action=actionenddefperform(*args)be

ruby - Ruby 方法可以接受 block 或参数吗?

我正在上TheOdinProject的类(class),现在我必须自己编写一个新的#count方法(使用另一个名称),它的行为与Enumerable模块中的普通方法一样。有关计数的文档说明如下(http://ruby-doc.org/core-2.4.0/Enumerable.html#method-i-count):count→intcount(item)→intcount{|obj|block}→intReturnsthenumberofitemsinenumthroughenumeration.Ifanargumentisgiven,thenumberofitemsinenumt

Ruby - 可以将 block 作为参数作为实际 block 传递给另一个函数吗?

这就是我想要做的:defcall_block(in_class="String",&block)instance=eval("#{in_class}.new")puts"instanceclass:#{instance.class}"instance.instance_eval{block.call}end#---TESTEXAMPLE---#Thisoutputs"class:String"everytime"sdlkfj".instance_eval{puts"class:#{self.class}"}#Thiswillonlyoutput"class:Object"everyti

ruby - 是否可以在 ruby​​ 的传递 block 中引用传递给方法的参数?

我希望我没有在这里重复任何人,但我一直在谷歌和这里搜索但没有想出任何东西。这个问题实际上更像是一个“性感化”我的代码的问题。我特别想做的是:Dir.new('some_directory').eachdo|file|#isthereawaytorefertothestring'some_directory'viaamethodorvariable?end谢谢! 最佳答案 一般不会;这完全取决于方法本身调用block的参数是什么,并且在each被调用(调用你的block)时,字符串'some_directory'被传递给Dir.new

ruby - 为什么 `block_given?` 在这个动态定义的方法中不起作用?

当我编写带有可选block的方法时,我通常使用类似block.callifblock_given?但是,在像下面这样动态定义的方法中,block_given?似乎不起作用。classFoo%w[barbaz].eachdo|method_name|define_singleton_method(method_name)do|&block|puts"Was#{method_name}givenablock?#{block_given?}"putsblock.callendendendFoo.bar{puts'Iamablock'}该block按预期调用,但block_given?返回fa

ruby-on-rails - 使用 rails send_data 发送 PDF

我使用ruby​​1.9.3和redmine1.4.4据此->Pleasehelpmesendajpgfileusingsend_data,我在Controller中这样做:@file=temp.pathFile.open(@file,'r')do|f|send_dataf.read,:filename=>"myfile.pdf",:type=>"application/pdf",:disposition=>"attachment"endFile.delete(@file)但它返回ArgumentError(UTF-8中的无效字节序列),为什么? 最佳答案

ruby - block 变量中的括号

给定a=[[:a,:b,:c]]1)我明白这一点a.each{|(x,y),z|pz}#=>:b有两个变量(x,y)和z,所以第三个元素:c被扔掉了,z匹配:b。我明白这一点a.each{|(x,y),z|py}#=>nil(x,y)匹配:a,因为它不是数组,所以它没有元素,所以y匹配nil。但是如何a.each{|(x,y),z|px}#=>:a工作?我希望返回nil。2)为什么返回值是这样的?a.each{|(x,y)|px}#=>:aa.each{|(x,y)|py}#=>:b我希望它们都返回nil。 最佳答案 这是因为并行赋

ruby-on-rails - Rake 文档 :app fails after upgrading to Ruby 2. 1.1 和 Rails 4.1

我使用命令rakedoc:app为我的Rails应用程序生成一些基本文档。它在过去一直运行良好。昨天我通过应用程序从Ruby1.9.3升级到2.1.1,从Rails3.2升级到4.1。该应用程序一切正常,所以几周后我第一次重新生成文档,但失败了。我运行上面的命令并收到以下错误消息:rakeaborted!Don'tknowhowtobuildtask'README.rdoc'/home/vagrant/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in`eval'/home/vagrant/.rvm/gems/ruby-2.1.1/

ruby-on-rails - rails : Omniauth - "The parameter app_id is required"

我正在按照这个railscast教程在我的rails项目上为facebook身份验证设置omniauth:http://railscasts.com/episodes/360-facebook-authentication?autoplay=true.我只用了4分钟,到目前为止我所做的就是捆绑gemomniauth-facebook并添加,omniauth.rbOmniAuth.config.logger=Rails.loggerRails.application.config.middleware.useOmniAuth::Builderdoprovider:facebook,ENV

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 最佳答案