Search code examples
rubydictionarydashing

dashing job undefined map method error


I'm using the dashing dashboard to display some data. Part of my code for one of the jobs uses the map! function in ruby:

vars = arr.map! { |element| element.gsub(/.{3}$/, '' )}

and when I try to run the dashboard using dashing start, I get the following error :

scheduler caught exception:
undefined method map! for #<Hash: 0x......>

If I run the code on its own as a ruby program, I get the correct result.


Solution

  • The documentation for the JSON module indicates that the parse method will "...convert your string into a hash." See http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html#module-JSON-label-Parsing+JSON.

    Try calling just map on your hash instead of calling map!:

    vars = arr.map { |element| element.gsub(/.{3}$/, '' )}
    

    The difference is that map will return a new array with the results of running your block once for every element in the Hash. Also, map is defined in the Enumerable module, which is included by Hash, but map! is not defined in Enumerable. See http://www.ruby-doc.org/core-2.0.0/Enumerable.html#method-i-map.