草庐IT

some_number

全部标签

ruby - 如何在 Ruby 中将整数舍入到 <nearest large number>?

假设我有以下任何一个数字:230957或83487或4785在Ruby中有什么方法可以将它们返回为300000或90000或分别是5000? 最佳答案 defround_up(number)divisor=10**Math.log10(number).floori=number/divisorremainder=number%divisorifremainder==0i*divisorelse(i+1)*divisorendend用你的例子:irb(main):022:0>round_up(4785)=>5000irb(main):

ruby-on-rails - 参数错误 : wrong number of arguments (1 for 2)

我是Rails、MVC和CRUD的新手,我正在尝试使用更新方法来更改帖子的投票数量。我的PostsController更新方法中有以下代码:defupdate@post=Post.find(params[:id])ifparams[:vote]=='up'@post.update_column(:ups=>@post[:ups]+1)elsifparams[:vote]=='down'@post.update_column(:downs=>@post[:downs]+1)endflash[:notice]="Thanksforvoting!Thishelpsusdetermineimp

ruby - 获取 `initialize' : wrong number of arguments(1 for 0) (ArgumentError) for simple ruby app

这是我的第一个ruby应用程序。我是一个堆栈溢出处女......当我运行以下程序时:classNameAppdefintialize(name)@names=[]enddefname_questionprint"Whatisyourname?"answer=gets.chomp@names+=answer.to_sputs"Thenumberofcharactersinyournameis"+names.lengthenddefname_lengthif@names.length>25thenprint"Yournameislongerthan25characters."elsepri

ruby-on-rails - 相当于 ruby​​/rails 中的 Array.some

我想在rails中做Array.some的等价物。这是一个应用于我的用例的示例,它是一种更复杂的include?(我想将其应用于*args):ary=[:a,:b,:c,d::x,e::y]#=>[:a,:b,:c,{:d=>:x,:e=>:y}]search=:econtained=ary.some{|x|x==search||x.try(:key?,search)}#=>trueassertcontained,"Weshouldhavefound#{search}"我可以用ary.map来做到这一点,但这意味着遍历整个数组然后再次测试它的内容。我还可以使用ary.drop_whil

ruby-on-rails - minitest_plugin.rb :9 getting wrong number of arguments

~/Sites/sample_app$railstestRunningviaSpringpreloaderinprocess24338Runoptions:--seed58780Running:..Finishedin0.292172s,6.8453runs/s,6.8453assertions/s./var/lib/gems/2.3.0/gems/railties-5.1.0/lib/rails/test_unit/minitest_plugin.rb:9:in`aggregated_results':wrongnumberofarguments(given1,expected0)(

ruby - 如何解决 factory_girl wrong number of arguments 错误

#rspectestcode@room=FactoryGirl.build(:room)#factorydefinitionfactory:roomdolength{10}width{20}end#codeimplementationclassRoomattr_accessor:length,:widthdefinitialize(length,width)@length=length@width=widthendend在尝试构建@room时运行rspec会导致此错误ArgumentError:wrongnumberofarguments(0for2) 最佳

ruby - Sinatra 请求 ["SOME_HEADER"] 不适用于 POST;文档错误?

Sinatra自述文件says:request["SOME_HEADER"]#valueofSOME_HEADERheader鉴于此应用:require'sinatra'post'/env'doenv['HTTP_X_FOO']endpost'/request'dorequest['X-Foo']endpost'/request_rack_http_format'dorequest['HTTP_X_FOO']end第一个规范通过;接下来的两个失败:describe"Sinatrashouldplacetheheaderin"dobefore(:all)doheader'X-Foo','

ruby-on-rails - 在 rails3 的 Controller 上使用 number_with_precision

我想在Controller上使用这个辅助方法。有什么办法可以实现吗? 最佳答案 可能不是一个好主意,但如果必须,请像这样包含帮助程序:classWhateverControllerincludeActionView::Helpers::NumberHelperdefshowrender:text=>number_with_precision(2342.234,:precision=>2)endend 关于ruby-on-rails-在rails3的Controller上使用number_

ruby - 为什么 Ruby/[[ :punct:]]/miss some punctuation characters?

ruby/[[:punct:]]/应该匹配所有“标点字符”。根据Wikipedia,这意味着/[\]\[!"#$%&'()*+,./:;?@\^_`{|}~-]/根据POSIX标准。匹配:-[]\;',./!@#%&*()_{}::"?.但是,它不匹配:=`~$^+|(至少在ruby​​1.9.3p194中是这样)。什么给了? 最佳答案 标点字符类由语言环境定义。公开组LC_TYPEdefinitionforpunct说:Definecharacterstobeclassifiedaspunctuationcharacters.In

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)