jjzjj

argument

全部标签

ruby - 使用 ( :send) in Ruby with keyword arguments?

我有一个私有(private)方法,我正尝试在Ruby中使用#sendto进行一些测试。该方法很复杂,我不想暴露在类之外,所以我想测试该方法,但我也不需要将其列为公共(public)方法。它有关键字参数。我怎样才能使用send来调用该方法,同时还向它传递关键字参数/命名参数?有办法吗?方法如下所示:defsome_method(keyword_arg1:,keyword_arg2:,keyword_arg3:nil) 最佳答案 取决于关键字参数的定义方式。如果出于某种原因将它们定义为内联,则将它们内联传递:SomeClass.sen

ruby - Rspec 模拟错误 : wrong number of arguments

我正在尝试使用Rspec对StripeAPI进行stub,但我遇到了一个问题。这是我的代码的样子:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)这是我遇到的错误:Failure/Error:Stripe::Customer.should_receive(:create).with(any_args).and_raise(Stripe::CardError)ArgumentError:wrongnumberofarguments(0for3..6)

ruby - 如何将参数从父任务传递到 Rake 中的子任务?

我正在编写一个Rake脚本,其中包含带参数的任务。我弄清楚了如何传递参数以及如何使任务依赖于其他任务。task:parent,[:parent_argument1,:parent_argument2,:parent_argument3]=>[:child1,:child2]do#PerformParentTaskFunctionalitiesendtask:child1,[:child1_argument1,:child1_argument2]do|t,args|#PerformChild1TaskFunctionalitiesendtask:child2,[:child2_argum

ruby-on-rails - 什么是 "circular argument reference"错误,有 activesupport time_zone?

我是ruby​​onrails的新手,我正在尝试创建一个教程。我在执行rakedb:migrate时遇到问题。hugo@ubuntu:~/pin_board$rakedb:migrate/home/hugo/.rvm/gems/ruby-2.2.2/gems/activesupport-4.0.5/lib/active_support/values/time_zone.rb:283:warning:circularargumentreference-now这是什么原因造成的?有人可以帮我吗? 最佳答案 您看到此警告消息是因为您的Ra

arrays - 如何通过 & :key as an argument to map instead of a block with ruby?

我写了这段代码:my.objects.map{|object|object.key}我的rubocop说:Pass&:keyasanargumenttomapinsteadofablock.有没有捷径可以做同样的事情? 最佳答案 Pass&:keyasanargumenttomapinsteadofablock意思是:my.objects.map(&:key) 关于arrays-如何通过&:keyasanargumenttomapinsteadofablockwithruby?,我们在S

ruby - 定义方法 : How to dynamically create methods with arguments

我想为find_by功能创建一堆方法。我不想一遍又一遍地写同样的东西,所以我想使用元编程。假设我想创建一个按名称查找的方法,接受名称作为参数。我该怎么做?我过去曾使用过define_method,但我没有为该方法采用的任何参数。这是我的(坏的)方法["name","brand"].eachdo|attribute|define_method("self.find_by_#{attribute}")do|attr_|all.eachdo|prod|returnprodifprod.attr_==attr_endendend有什么想法吗?提前致谢。 最佳答案

ruby - "wrong number of arguments (1 for 0)"在 Ruby 中是什么意思?

“参数错误:参数数量错误(1代表0)”是什么意思? 最佳答案 当您定义一个函数时,您还定义了该函数需要工作的信息(参数)。如果它被设计为在没有任何额外信息的情况下工作,并且你传递了一些信息,你就会得到那个错误。例子:不接受参数:defdogend接受参数:defcat(name)end当你调用它们时,你需要用你定义的参数来调用它们。dog#worksfinecat("Fluffy")#worksfinedog("Fido")#ReturnsArgumentError(1for0)cat#ReturnsArgumentError(0f

javascript - Array.prototype.slice.call(arguments) 与 Array.prototype.slice.apply(arguments)

上一个posts已经讨论过Array.prototype.slice.call(arguments)是如何工作的,但我不明白你为什么使用call而不是apply当apply用于类似数组的对象时,而call用于以逗号分隔的对象列表。arguments不是应该使用apply而不是call的类数组对象吗? 最佳答案 如果您想将参数传递给数组中的slice而不是一个一个地传递,那就有区别了。你可以这样做[1,2,3,4,5,6,7]----ourexampleargumentsArray.prototype.slice.call(argum

JavaScript 约定 : How do you assign arguments to the parent scope

varproblemtest=function(){varparameters;returnfunction(parameters){parameters=parameters;}}varmysolutiontest=function(){varparameters;returnfunction(parametersIn){parameters=parametersIn;}}这更像是一个JavaScript约定问题。通常我在上面有类似的代码。函数接受参数并将其分配给父范围。但是,我不能像在problemtest中那样使用它,因为作为参数的parameters隐藏了problemtest

javascript - 语法错误 : invalid arrow-function arguments (parentheses around the arrow-function may help)

这样的代码会产生一个错误:if(hr>t1[0]||(hr==t1[0]&&min=>t1[1])&&hr错误:SyntaxError:无效的arrow-function参数(arrow-function周围的括号可能有帮助)这是什么意思,它是如何发生的?Google搜索此错误毫无用处。编辑:似乎是使用=>=而不是=引起的。但我仍然很好奇为什么错误是这样表述的,以及箭头函数应该是什么。编辑2.首先,我没有意识到这实际上可能是特定于浏览器的问题。另外,我没有意识到现在人们在浏览器上下文之外的其他地方使用JS。所以,为了说明这一点,我的浏览器是MozillaFirefox25.0.1。