ruby - Most efficient way to format a hash of data? -


I am using this array of hashes to batch in a Mongo DB. Each hash was populated by parsing a text file so that the formatting of the field was in an unexpected format. Something like this might look:

  {date => "March 5", time => "05:22:21", first_name = "John", middle_initial = "JJ", ...}   

And I have a series of formatting tasks. Then it may be: def format_date .. if necessary, convert. End df format_time ... end

How do I make the formatting function Will the different records go about making calls? I can call any type of lambda where I work again via hash and I call the format_record_name function, but all records will not have formatting functions. For example any one of the first_name records will not need one?

This is an idea that is similar to what you have said. You can also create an identity function for those fields that you do not want to format.

  def pass (x) x end method_hash = {: date = & gt; Method (: your_format_date)} method_hash default = method (: pass) x = {: date = & gt; "March 5",: time => "05:22:21",: first_name = & gt; "John" ,: middle_initial = & gt; "J j"} x.reduce ({}) {| Hsh, k | Hsh [k [0]] = method_hash [k [0]]. Call (k [1]); Hsh}    

Comments