草庐IT

while-else

全部标签

ruby - 为什么尾递归 gcd 比 rubinius 的 while 循环更快

我有这两个gcd函数的实现:defgcd1(a,b)ifa==baelsifa>bif(a%b)==0belsegcd1(a%b,b)endelseif(b%a)==0aelsegcd1(a,b%a)endendenddefgcd2(a,b)if(a==b)returnaelsifb>amin,max=a,belsemin,max=b,aendwhile(max%min)!=0min,max=max%min,minendminend函数gcd1是尾递归的,而gcd2使用while循环。我已经验证rubinius通过对阶乘函数进行基准测试来执行TCO,只有阶乘函数基准测试显示递归版本和迭

ruby-on-rails - 目录 : template missing error while rendering JSON

我的Controller中有以下代码:deftljson(result=[])@stat_id=params[:stat_id]@rpm=FedoraRpm.find_by_name(@stat_id)@rpm.ruby_gem.historical_gems.each{|h|resulth.version,:start=>h.build_date}}@rpm.bugs.each{|b|resultb.name+"ViewonBugZilla",:start=>b.bz_id}}@res=result.to_jsonrespond_todo|format|format.json{ren

Ruby 习语 : method call or else default

在Ruby中执行此操作的正确方法是什么?defcallOrElse(obj,method,default)ifobj.respond_to?(method)obj.__send__(method)elsedefaultendend 最佳答案 因为还没有提供答案:result=obj.methodrescuedefault与@slhck一样,当我知道obj很有可能不会响应方法时,我可能不会使用它,但这是一个选项。 关于Ruby习语:methodcallorelsedefault,我们在St

ruby - 错误 : inreplace failed while installling with homebrew?

我想安装treetagger在OSX中。为了使它更容易,我尝试搜索Homebrew是否可行。所以我在网上找到了这个formula来自pepijnkokke用户。接下来,我尝试按如下方式安装treetagger:user@MacBook-Pro-User-2:~$brewinstall/Users/user/Downloads/tree-tagger.rb但是,我得到了以下错误:==>Installingdependenciesfortree-tagger:openssl,wget==>Installingtree-taggerdependency:openssl==>Downloadi

ruby-on-rails - ruby rails : Removing a dependency while installing a gem?

我正在尝试在我的Windows机器上安装twitter-bootstrap-rails。这个gem依赖于therubyracer,它还不兼容Windows。我发现了这个:therubyracergemonwindows这告诉我,我应该只能依靠JScript。虽然我不知道如何告诉gem安装忽略依赖项。我试图强制安装:geminstall-ftwitter-bootstrap-rails--platformruby没有用。有没有人我怎么能说“geminstalltwitter-bootstrap-rails[但忽略对therubyracer的依赖]”? 最佳答案

ruby - Rubocop 保护子句困境 - 不必要 if else VS 行太长保护子句

我有一段代码,其中有一个带有保护子句的raise语句:defvalidate_indexindex#ChangetoSizeErrorraiseArgumentError,"Sizeofindex(#{index.size})doesnotmatches"\"sizeofvector(#{size})"ifsize!=index.sizeend在这一点上,rubocop给出了罪行:Style/MultilineIfModifier:Favoranormalif-statementoveramodifierclauseinamultilinestatement.我将我的代码修改为正常if

ruby - 我可以在 Ruby 的 if/else 中使用花括号吗?

为什么我不能在if/else结构中使用大括号?我离开了Python,因为我不习惯仔细地缩进语句。在Ruby中也是这样吗?比如我可以这样写吗?iftoken=="hello"{puts"helloencountered"#lotsoflineshere}有没有办法使用大括号来做到这一点?我也阅读了有关block的内容,但不确定如何在if/else表达式中使用它们。 最佳答案 您不能使用大括号,但缩进也无所谓。Ruby使用end关键字代替右大括号。iftoken=="hello"puts"helloencountered"#lotsof

ruby-on-rails - 问号和冒号 - if else in ruby

您好,我有一个关于ruby​​onrails的问题显然我有这样的声明:defsort_columnProduct.column_names.include?(params[:sort])?params[:sort]:"name"end据我所知,据说此方法根据参数[:sort]对列进行排序,如果没有参数,产品将按“名称”排序。但是,我不明白这个语句的写法,尤其是第二个“?”。有人可以向我解释吗? 最佳答案 这是您的代码,重新排列以便于理解。defsort_columncond=Product.column_names.include?

ruby-on-rails - 'EOFError : end of file reached' on HEROKU while posting UTF-8 via SSL

我在heroku上有一个奇怪的错误。要重现它,我必须在请求正文中使用任何UTF-8字符制作大的(超过几KB)HTTPSPOST。这是一个例子:require"net/https"require"uri"#AccutallyI'veecounteredthisbugwhilepostingtoanotherserverurl=URI.parse("https://api.heroku.com/myapps")#It'sUkrainian'oicedvelarplosiveG'letterpayload="ґ"*10000request=Net::HTTP::Post.new(url.pa

ruby - if + else 和 if -> unless 不一致

我今天在定义我解决的自定义RSpec匹配器时遇到了一个问题,但实际上看不出任何一种方法有效而另一种方法无效的原因,这是代码:方法1——if+else:RSpec::Matchers.define:have_success_messagedo|message|matchdo|page|ifmessage.nil?page.shouldhave_selector('div.alert.alert-success')elsepage.shouldhave_selector('div.alert.alert-success',text:message)endendend方法2--if后跟unl