Ruby: delete everything after (including) decimal

I have this line: "37:48.1234567" (there is a colon and there is a decimal number)

I need to remove everything from decimal and past.

The number before the decimal place can be any length (i.e. 1:23:39.12357 ).

+4
source share
3 answers
 str.split(".")[0] 

Hope this helps!

+13
source

I have made a small guideline with decisions so far. I changed the decision a bit.

One incomprehensible problem: What is the result for 1:23:39.123.57 ? 1:23:39 or 1:23:39.123 ?

I chose 1:23:39 , only the test regex has a different solution (see the answer to the question about tims )

The split option has 2x2 options: [0] or .first and is divided into the 2nd parameter , 2 . The second separation parameter limits the number of divided elements. We only need the first, so we can first split up and relax.

The decision to fast: split('.', 2) . If you use first or [0] , it does not matter. I would prefer first.

Result ( ruby 1.9.2p290 ):

 Rehearsal ------------------------------------------------- regex 0.141000 0.000000 0.141000 ( 0.140625) regex2 0.125000 0.000000 0.125000 ( 0.125000) split[0] 0.031000 0.000000 0.031000 ( 0.031250) split[0],2 0.047000 0.000000 0.047000 ( 0.046875) split.first 0.031000 0.000000 0.031000 ( 0.031250) split.first,2 0.031000 0.000000 0.031000 ( 0.031250) ---------------------------------------- total: 0.406000sec user system total real regex 0.125000 0.000000 0.125000 ( 0.125000) regex2 0.109000 0.015000 0.124000 ( 0.125000) split[0] 0.031000 0.000000 0.031000 ( 0.031250) split[0],2 0.032000 0.000000 0.032000 ( 0.031250) split.first 0.047000 0.000000 0.047000 ( 0.046875) split.first,2 0.031000 0.000000 0.031000 ( 0.031250) 

Result ( ruby 1.8.7p352 ):

 Rehearsal ------------------------------------------------- regex 0.060000 0.000000 0.060000 ( 0.060661) regex2 0.050000 0.000000 0.050000 ( 0.049141) split[0] 0.080000 0.000000 0.080000 ( 0.083703) split[0],2 0.070000 0.000000 0.070000 ( 0.072780) split.first 0.080000 0.000000 0.080000 ( 0.080419) split.first,2 0.070000 0.000000 0.070000 ( 0.068370) ---------------------------------------- total: 0.410000sec user system total real regex 0.060000 0.000000 0.060000 ( 0.055427) regex2 0.050000 0.000000 0.050000 ( 0.048538) split[0] 0.080000 0.000000 0.080000 ( 0.084225) split[0],2 0.070000 0.000000 0.070000 ( 0.071673) split.first 0.070000 0.000000 0.070000 ( 0.079865) split.first,2 0.060000 0.000000 0.060000 ( 0.068685) 

Test:

 require 'benchmark' TESTDATA = %w{ 37:48.1234567 1:23:39.12357 1:23:39.123.57 } N = 10_000 #Number of Test loops Benchmark.bmbm(10) {|b| b.report('regex') { N.times { TESTDATA.each{|str| str.gsub(/\.[^.]*\Z/, '') }} } b.report('regex2') { N.times { TESTDATA.each{|str| str.gsub(/\..*\Z/, '') }} } b.report('split[0]') { N.times { TESTDATA.each{| str | str.split(".")[0] }} } b.report('split[0],2') { N.times { TESTDATA.each{| str | str.split(".",2)[0] }} } b.report('split.first') { N.times { TESTDATA.each{| str | str.split(".").first }} } b.report('split.first,2') { N.times { TESTDATA.each{| str | str.split(".",2).first }} } #~ b.report('') { N.times { TESTDATA.each{| str | }} } #~ b.report('') { N.times { TESTDATA.each{|t| }} } #~ b.report('') { N.times { TESTDATA.each{|t| }} } } #Benchmark 
+4
source
 result = subject.gsub(/\.[^.]*\Z/, '') 

does just that. It ensures that if there are several decimal places, only the latter will be affected.

+3
source

Source: https://habr.com/ru/post/1390917/


All Articles