在Ruby中将范围转换为浮点值数组时出错

brg*_*brg 4 ruby arrays range

如何将Range having startendinterval 转换为Float值?我收到错误了TypeError: can't iterate from Float

IRB会议

irb(main):058:0> (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

irb(main):059:0> ('a'..'k').to_a
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]

irb(main):061:0> ((1.1)..(1.10)).to_a
TypeError: can't iterate from Float
    from (irb):61:in `each'
    from (irb):61:in `to_a'
    from (irb):61
    .........
Run Code Online (Sandbox Code Playgroud)

Agi*_*gis 7

试试这个:

(1.1..1.2).step(0.01).map { |x| x.round(2) }
  # => [1.1, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2]
Run Code Online (Sandbox Code Playgroud)