草庐IT

pattern-matching

全部标签

ruby - 使用 ruby​​ 识别阵列上的运行

如果我们有一个数组array=[1,1,0,0,2,3,0,0,0,3,3,3]我们如何识别给定数字的运行(具有相同值的连续数字的数量)?例如:run_pattern_for(array,0)->2run_pattern_for(array,3)->1run_pattern_for(array,1)->1run_pattern_for(array,2)->0没有2的运行,因为没有连续出现2。3有一个运行,因为只有一个幻影以树为连续数字。 最佳答案 尝试:classArraydefcount_runs(element)chunk{|n

ruby - 为什么 `Symbol#match` 的行为与 `String#match` 和 `Regexp#match` 不同?

String#match和Regexp#match在匹配成功时返回一个MatchData:"".match(//)#=>#//.match("")#=>#//.match(:"")#=>#但是Symbol#match返回匹配位置(如String#=~)::"".match(//)#=>0为什么Symbol#match表现不同?有用例吗? 最佳答案 我将其报告为Ruby核心中的错误:https://bugs.ruby-lang.org/issues/11991.让我们看看他们会怎么说。更新被质疑的行为似乎是一个错误。似乎从Ruby2.

ruby , `match' : invalid byte sequence in UTF-8

我对UTF-8编码有一些问题。我在这里阅读了一些帖子,但它仍然无法正常工作。这是我的代码:#!/bin/envruby#encoding:utf-8defdeterminefile=File.open("/home/lala.txt")file.eachdo|line|puts(line)type=line.match(/DOG/)puts('aaaaa')iftype!=nilputs(type[0])breakendendend这是我文件的前3行:;?lalalalal60000065535-1362490443-0000006334-0000018467-0000000041en

ruby-on-rails - rails : replacing try with the Null Object Pattern

在我的大多数应用程序中,我都有一个current_user方法。为了避免像current_user.name这样的情况出现异常,其中current_user是nil,rails提供了try方法。这个问题是我需要记住在current_user可能是nil的地方使用try。我想使用NullObject模式来消除这种额外的开销。classNullUserdefmethod_missing(method_name,*args)nilendenddefcurrent_userreturnNullUser.newunlessUserSession.find@current_user||=UserS

具有多个变量的 Ruby case 语句

Ruby有一个相当强大的case..when..else构造,用于在需要将条件与单个变量进行匹配时使用。无需简单地嵌套case语句即可将条件与多个变量进行匹配的“规范”方法是什么?将多个变量包装在一个数组中(如[x,y])并与其匹配并不等价,因为Ruby不会应用神奇的case===运算符到数组的元素;该运算符仅应用于数组本身。我将继续使用社区维基答案来回应这个问题(失败)。 最佳答案 您需要使用if..elsif..else,并确保要匹配的变量出现在===的右侧运算符(这就是case本质上所做的)。例如,如果你想匹配x和y针对某些标

No loop matching the specified signature and casting was found for ufunc greater

目录报错信息np.greater学习临时解决方法:np.greater去掉dtype报错信息pipinstallnumpy==1.24报错代码:dda=np.cumsum(np.greater(counts,0),dtype=np.int32)print(dda)Noloopmatchingthespecifiedsignatureandcastingwasfoundforufuncgreaternp.greater学习1.函数功能:判断参数一是否大于参数二。2.参数介绍  arr1:第一个参数类似一个数组  arr2:第二个参数类似一个数组  out:返回值是bool类型或者是元素为bool

ruby-on-rails - Rails 4 路由错误 : No Route Matches [POST]

我在学习Rails4时正在做一个小练习,但在尝试更新对象时遇到路由错误。我不断收到错误消息:没有路由匹配[POST]"/movies/1/edit"但看不到我的代码哪里不正确:我的电影_controller.rbclassMoviesController"Yourmoviewassaved!"elserender"new"endenddefedit@movie=Movie.find(params[:id])enddefupdate@movie=Movie.find(params[:id])if@movie.update_attributes(params[:movie])redirec

ruby-on-rails - rails 引擎 : rake routes show its routes but on rspec execution "No route matches" is thrown

我正在尝试测试我的应用程序正在使用的引擎内部的Controller。规范不在引擎中,而是在应用程序本身中(我试图在引擎中进行测试,但也遇到了问题)。我的引擎有以下routes.rb:Revision::Engine.routes.drawdoresources:steps,only:[]docollection{get:first}endend引擎正常挂载在应用routes.rb上:mountRevision::Engine=>"revision"当我运行rakeroutes时,在最后一行我得到:RoutesforRevision::Engine:first_stepsGET/step

ruby - 安装 RDoc 文档时出错 : incompatible encoding regexp match

上周,在一些gem安装或gem更新操作中,我收到了与这些类似的错误:ERROR:Whilegeneratingdocumentationforsinatra-1.3.1...MESSAGE:errorgeneratingC:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/doc/sinatra-1.3.1/rdoc/README_de_rdoc.html:incompatibleencodingregexpmatch(UTF-8regexpwithIBM437string)(Encoding::CompatibilityError)类似的:E

ruby 正则表达式 : ^ matches start of line even without m modifier?

ruby1.8.7。我正在使用带有^的正则表达式来匹配字符串开头的模式。问题是,如果在字符串中任何行的开头找到模式,它仍然匹配。如果我使用“m”修饰符但我没有使用,这是我期望的行为:$irbirb(main):001:0>str="hello\ngoodbye"=>"hello\ngoodbye"irb(main):002:0>putsstrhellogoodbye=>nilirb(main):004:0>str=~/^goodbye/=>6我在这里做错了什么? 最佳答案 行首:^行尾:$字符串的开头:\A字符串结尾:\z