使用Ruby将输入值转换为整数或浮点数

Wat*_*Box 5 ruby numbers

我相信我对这个问题有一个很好的答案,但我想确保ruby-philes没有更好的方法来做到这一点.

基本上,给定一个输入字符串,我想在适当的情况下将字符串转换为整数,或者在适当的时候将其转换为浮点数.否则,只需返回字符串.

我将在下面发布我的答案,但我想知道是否有更好的方法.

例如:

to_f_or_i_or_s("0523.49") #=> 523.49
to_f_or_i_or_s("0000029") #=> 29
to_f_or_i_or_s("kittens") #=> "kittens"
Run Code Online (Sandbox Code Playgroud)

jar*_*ine 9

我会尽可能避免在Ruby中使用正则表达式.这是出了名的慢.

def to_f_or_i_or_s(v)
  ((float = Float(v)) && (float % 1.0 == 0) ? float.to_i : float) rescue v
end

# Proof of Ruby's slow regex
def regex_float_detection(input)
  input.match('\.')
end

def math_float_detection(input)
  input % 1.0 == 0
end

n = 100_000
Benchmark.bm(30) do |x|
  x.report("Regex") { n.times { regex_float_detection("1.1") } }
  x.report("Math") { n.times { math_float_detection(1.1) } }
end

#                                     user     system      total        real
# Regex                           0.180000   0.000000   0.180000 (  0.181268)
# Math                            0.050000   0.000000   0.050000 (  0.048692)
Run Code Online (Sandbox Code Playgroud)

一个更全面的基准:

def wattsinabox(input)
  input.match('\.').nil? ? Integer(input) : Float(input) rescue input.to_s
end

def jaredonline(input)
  ((float = Float(input)) && (float % 1.0 == 0) ? float.to_i : float) rescue input
end

def muistooshort(input)
  case(input)
  when /\A\s*[+-]?\d+\.\d+\z/
      input.to_f
  when /\A\s*[+-]?\d+(\.\d+)?[eE]\d+\z/
      input.to_f
  when /\A\s*[+-]?\d+\z/ 
      input.to_i     
  else  
      input
  end
end

n = 1_000_000
Benchmark.bm(30) do |x|
  x.report("wattsinabox") { n.times { wattsinabox("1.1") } }
  x.report("jaredonline") { n.times { jaredonline("1.1") } }
  x.report("muistooshort") { n.times { muistooshort("1.1") } }
end

#                                     user     system      total        real
# wattsinabox                     3.600000   0.020000   3.620000 (  3.647055)
# jaredonline                     1.400000   0.000000   1.400000 (  1.413660)
# muistooshort                    2.790000   0.010000   2.800000 (  2.803939)
Run Code Online (Sandbox Code Playgroud)


Wat*_*Box 5

def to_f_or_i_or_s(v)
    v.match('\.').nil? ? Integer(v) : Float(v) rescue v.to_s
end
Run Code Online (Sandbox Code Playgroud)