Ruby 从数组中删除非数值并将 rest 转换为 float

Ruby removing non numeric values from array and converting rest to float

我将用户输入的字符串转换为数组,然后遍历该数组并删除非数字值。

看起来我的正则表达式匹配有一半时间有效,而我的 to_f 从未将数组值设置为浮点数。

Lets assume I enter: "1 2 3b c3 4, 5t"

puts "Enter Minutes"  
STDOUT.flush  
freq = gets.chomp
freq = freq.split(/\W/) #this creates the array, splitting at non-word chars

p freq #outputs: ["1", "2", "3b", "c3", "4", "", "5t"]

freq.each do |minutes|
        if ( minutes == "" or /\D/.match(minutes) ) then freq.delete(minutes) else minutes.to_f end
end

p freq #outputs: ["1", "2", "c3", "4", "5t"]

My desired results are: [1, 2, 4] #note they are numeric not chars

问题是你只在 then 条件下改变 freq 而不是在 else 条件下。

有可枚举的方法为您改变,它们通常以 !:

结尾
freq = ["1", "2", "3b", "c3", "4", "", "5t"]
=> ["1", "2", "3b", "c3", "4", "", "5t"]

freq.reject! { |minutes| minutes.match(/\D/) || minutes == "" }.map! { |minutes| minutes.to_f }
=> [1.0, 2.0, 4.0]
arr = ["1", "2", "3b", "c3", "4", "", "5t"]

这个问题有两种可能的解决方法。一种是执行两个步骤:删除所有不是非负整数表示的元素,然后将这些整数转换为浮点数,需要遍历两个数组中的每一个。另一种是构建一个浮点数数组,单次通过arr。可以按如下方式完成。

arr.each_with_object([]) { |s,a| a << s.to_f if s.match?(/\A\d+\z/) }
  #=> [1.0, 2.0, 4.0] 

正则表达式为,“匹配字符串的开头(锚点\A),后跟一个或多个数字,然后是字符串的结尾(锚点\z) . 这与不匹配空字符串或非数字字符相同,因此我们可以改为:

arr.each_with_object([]) { |s,a| a << s.to_f unless s.empty? or s.match?(/\D/) }
  #=> [1.0, 2.0, 4.0] 

这里在两者之间没有太多选择,但有时后一种方法更容易实现。

我不太喜欢(但值得了解)的另一种方法是使用方法 Kernel#Float followed by Array#compact:

arr = ["1", "-2", "3b", "c3", "4.23", "", "5t", "-1.2e3"]

arr.map { |s| Float(s) rescue nil }.compact
  #=> [1.0, -2.0, 4.23, -1200.0]

如上所示,这会将 arr 中代表整数或浮点数(不仅仅是非负整数)的元素转换为浮点数(可能需要也可能不需要)。

Float(s) 如果 s 无法转换为浮点数,则引发 ArgumentError。例如:

Float("3b")
  #=> ArgumentError (invalid value for Float(): "3b")

发生这种情况时,我在 内联救援子句 中捕获异常,返回 nil

一些 Rubies 不喜欢内联救援子句,因为它们可以掩盖其他错误。只有拯救 ArgumentErrors:

才能解决这个问题
arr.map do |s|
  begin
    Float(s)
  rescue ArgumentError
    nil
  end
end.compact
  #=> [1.0, -2.0, 4.23, -1200.0]