ruby - YAML file range, converting string to array throwing undefined method `to_a' -


i have yaml file contains bunch of ranges values:

combat_power:   bulbasaur: (12.2..1071)   ivysaur: (19.5..1632)   venusaur: (32.2..2580)   charmander: (10.7..955)   charmeleon: (18.5..1557)   charizard: (32.5..2602)   squirtle: (11.4..1008)   wartortle: (18.9..1582)   blastoise: (31.7..2542)   caterpie: (4.3..443)   metapod: (4.7..477)   butterfree: (17.2..1454) 

while attempting turn these ranges array getting error: <main>': undefined method to_a'

how attempting simple:

require 'yaml'  data = yaml.load_file('./lib/lists/yamls/combat_power.yml')  print 'enter name: ' name = gets.chomp.capitalize  new_data = data['combat_power'][name]  puts new_data.to_a 

when run:

enter name: charmeleon go.rb:10:in `<main>': undefined method `to_a' "(18.5..1557)":string (nometho derror) did mean?  to_yaml                to_f                to_r                to_i                to_s                to_c 

my question being, how turn range key array when being provided yaml file? other way can turn range array using to_a function, doesn't appear working. there simple solution missing, or ranges not allowed used in yaml files?

a few things:

  1. ruby's yaml implementation doesn't unserialize ruby objects without explicitly telling so. both security feature (some code dangerous unserialize), impossible task accomplish (how supposed know intend evaluated?). so, turn range string actual ruby range object, need eval it:

    require 'yaml'  data = yaml.load_file('./yamls.yaml')  print 'enter name: ' name = gets.chomp.capitalize  new_data = data['combat_power'][name]  puts eval(new_data).class # => range puts eval(new_data).to_a 

    note should eval text explicitly wrote--evaling code random source, such user input, can serious security issue. long evaling values yaml file wrote, should fine.

  2. ruby doesn't know how enumerate range of floats, (17.2..1454).to_a throw typeerror. because interpreter can't possibly know if want increment +1, +0.1, etc. i'm not sure how want solve issue, since solution based on meaning of data specifically.


Comments