草庐IT

line-numbers

全部标签

ruby parslet : parsing multiple lines

我正在寻找一种匹配多行Parslet的方法。代码如下所示:rule(:line){(match('$').absent?>>any).repeat>>match('$')}rule(:lines){line.repeat}但是,lines将始终以无限循环结束,这是因为match('$')将无休止地重复以匹配字符串的结尾。是否可以匹配可以为空的多行?irb(main)>lines.parse($stdin.read)Thisisamultilinestring^D应该匹配成功。我错过了什么吗?我还尝试了(match('$').absent?>>any.maybe).repeat(1)>>

ruby - 如何将字符串格式的毫秒数转换为 HH :MM:SS format in Ruby in under 3 lines of code?

@scores_raw.eachdo|score_raw|#belowiscodeiftimewasbeingsentinmillisecondshh=((score_raw.score.to_i)/100)/3600mm=(hh-hh.to_i)*60ss=(mm-mm.to_i)*60crumbs=[hh,mm,ss]sum=crumbs.first.to_i*3600+crumbs[1].to_i*60+crumbs.last.to_i@scoressum,:hms=>hh.round.to_s+":"+mm.round.to_s+":"+ss.round.to_s}@score

ruby - Rubocop 规则 : Never use 'do' with multi-line 'while

我有以下代码#coloursarandomcellwithacorrectcolourdefcolour_random!whiletruedocol,row=rand(columns),rand(rows)cell=self[row,col]ifcell.empty?thencell.should_be_filled??cell.colour!(1):cell.colour!(0)breakendendend做什么并不重要,尽管它应该很明显。关键是Rubocop给了我一个警告Neveruse'do'withmulti-line'while为什么我不应该那样做?那我该怎么办呢?

ruby - 调用 instance_eval(&lambda) 传递当前上下文时出现错误 'wrong number of arguments'

要清楚-此代码运行完美-codewithproc但如果我将Proc.new更改为lambda,则会出现错误ArgumentError:wrongnumberofarguments(1for0)这可能是因为instance_eval想要将self作为参数传递,而lambda将其视为一种方法并且不接受未知参数?有两个例子-第一个是工作:classRuledefget_ruleProc.new{putsname}endendclassPersonattr_accessor:namedefinit_rule@name="ruby"instance_eval(&Rule.new.get_rule

ruby (在 Rails 上)正则表达式 : removing thousands comma from numbers

这看起来很简单,但我遗漏了一些东西。我有大量来自各种来源和不同格式的输入。数字输入123123.45123,45(notethecommausedheretodenotedecimals)1,2341,234.5612,345.6712,345,67(notethecommausedheretodenotedecimals)关于输入的附加信息数字永远小于100万编辑:这些是价格,因此要么是整数,要么是百分之一我正在尝试编写一个正则表达式并使用gsub去除千位逗号。我该怎么做?我写了一个正则表达式:myregex=/\d+(,)\d{3}/当我在Rubular中测试它时,它表明它只在我想

ruby - 在 ruby​​ 中,file.readlines.each 并不比 file.open.each_line 快,为什么?

只是为了分析我的iis日志(奖励:碰巧知道iislog是用ASCII编码的,errrr..)这是我的ruby代码1.readlinesDir.glob("*.log").eachdo|filename|File.readlines(filename,:encoding=>"ASCII").eachdo|line|#commentlineifline[0]=='#'nextelseline_content=line.downcase#justcareaboutfirstonematched_keyword=keywords.select{|e|line_content.include?e

ruby each_line 也读取换行符?

我正在尝试从文本文件中读取数据并将其与帖子字符串连接起来。当文件中只有一行时,它工作正常。但是有2行,我的请求失败了。each_line是否读取换行符?我该如何纠正它?File.open('sfzh.txt','r'){|f|f.each_line{|row|send(row)}我确实通过拆分和额外的定界符绕过了这个问题。但它看起来很丑。 最佳答案 是的,each_line包括换行符。但是您可以使用chomp轻松剥离它们:File.foreach('test1.rb')do|line|sendline.chompend

ruby - 参数错误 : wrong number of arguments (1 for 0) when using afer_save

ArgumentError:wrongnumberofarguments(1for0)from/Users/Castillo/Desktop/gainer/app/models/status.rb:13:in`update_remaining_nutrients'from/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:507:in`blockincallback'from/usr/local/rvm/

ruby-on-rails - "if (number in range) then..."的 Ruby 快捷方式

是否有以下Ruby快捷方式?if(x>2)and(x我以为我看到了类似的东西,但找不到相关的引用资料。当然,当您不知道要查找的运算符时,很难查找。 最佳答案 if(3..9).include?x#whateverend作为旁注,您还可以对范围使用三等号运算符:if(3..9)===x#whateverend这让您也可以在case语句中使用它们:casexwhen3..9#Dosomethingwhen10..17#Dosomethingelseend 关于ruby-on-rails-"i

ruby - "wrong number of arguments"使用圆形时出现 ArgumentError

我正在尝试将温度从华氏度转换为摄氏度:puts'ConvertirgradosFahrenheitaCelcius'STDOUT.flushx=gets.chompaprox=(x*100.0).round(2)/100.0resultado=(aprox-32)/1.8putsresultado我使用正确的公式将华氏度转换为摄氏度:Celsius=Fahrenheit-32/1.8但是,当我在控制台中运行它时,出现以下错误:`round':wrongnumberofarguments(1for0)(ArgumentError)我尝试过不同的方法,但我不明白为什么这不起作用。