草庐IT

number-sequence

全部标签

ruby-on-rails - Rails number_to_currency 删除小数点右边的尾随零

我有一个动态生成的表格,它乘以价格*数量。部分价格以美分计算。例如如果某件商品的价格是0.0375,我可以在我的表格中将其显示为number_to_currency(0.0375,:precision=>4)=>$0.0375但是我得到的价格是标准的2位小数的数量number_to_currency(33.95,:precision=>4)=>$39.9500我需要一种方法来去除十进制值的尾随零。请记住,输出位于Model.eachblock中,因此我不确定是否可以有条件地修改精度参数。 最佳答案 尝试指定strip_insigni

ruby 任务 : joining numbers to intervals

我有一组唯一编号。像这样:[1,2,3,4,7,8,10,12]。它可以是未排序的。我需要的是获取此数组的间隔:intervals_for[1,2,3,4,7,8,10,12]#=>"1-4,7-8,10,12"我有自己的解决方案:defintervals_for(array)array.sort!new_array=[]array.eachdo|a|ifnew_array.lastanda==new_array.last.last+1new_array.last1?"#{a.first}-#{a.last}":a.first}.join(",")end但我认为这里有更干净的解决方案

ruby 1.9.2 : irb throws ArgumentError: invalid byte sequence in UTF-8 when entering German Umlaut

我想在我的irb中输入德语变音符号,但出现奇怪的错误。我可以毫无问题地输入äöü的任何字符,但是每个ÄÖÜß都会导致以下错误:$irbruby-1.9.2-p136:001>?#hereIenteredÜbutitdisplaysonly?/Users/lorenz/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/irb/ruby-lex.rb:728:in`blockinlex_int2':invalidbytesequenceinUTF-8(ArgumentError)我已经查看了很多关于Ruby、rvm和UTF-8的SO问题,但都没有帮助。大

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 - 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 - String#encode 未修复 "invalid byte sequence in UTF-8"错误

我知道有很多关于此错误的类似问题,而且我已经尝试了很多,但都没有成功。我遇到的问题涉及字节\xA1并且正在抛出ArgumentError:invalidbytesequenceinUTF-8我尝试了以下但没有成功:"\xA1".encode('UTF-8',:undef=>:replace,:invalid=>:replace,:replace=>"").sub('','')"\xA1".encode('UTF-8',:undef=>:replace,:invalid=>:replace,:replace=>"").force_encoding('UTF-8').sub('','')"

ruby - 正则表达式 : Any characters except sequence

[^abc]Anysinglecharacterexcept:a,b,orc但是我如何为除序列abc之外的任何字符制作正则表达式所以,类似的东西"Helloabcawesomeworld".scan/[^(abc)]+/将返回“Hello”和“awesomeworld”。PS:而且不是分割字符串 最佳答案 这叫做lookaround,在您的情况下,您需要使用负前瞻。我不确定Ruby中的确切语法,但(?!abc)中的某些内容可能会起作用。请注意,lookaround不会消耗任何输入,因此您需要在其后跟任何您想要匹配的模式。也许(?:(