Skip to content
Format Array data to a hash with your definition. it also can parse the CSV file with definition of title.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Gem Version Build Status

Format Array data to a hash with your definition. it also can parse the CSV file with definition of title.


Add this line to your application's Gemfile:

gem 'array_hasher'

And then execute:

$ bundle

Or install it yourself as:

$ gem install array_hasher


Format array

New a formatter

require 'array_hasher'

f = ArrayHasher.new_formatter([
  # [hash_key, data_type]
  [:a, :int],
  [:b, :float],
  [:c, proc {|v| v.split(',') }],
  [:d, nil, range: 3..-1]

Array to hash by formatter

f.parse(['number: 123', '$ 123.1', 'a,b,c', 'd1', 'd2', 'd3'])
# => {a: 123, b: 123.1, c: ['a', 'b', 'c'], d: ['d1', 'd2', 'd3']}

Define your data type

f.define_type(:my_arr) {|v| v.split(',').map(&:to_i) }
f.cols[2] = [:c, :my_arr]
f.parse(['number: 123', '$ 123.1', '1,2,3', 'd1', 'd2', 'd3'])
# => {a: 123, b: 123.1, c: [1, 2, 3], d: ['d1', 'd2', 'd3']}

Format CSV

For a CSV file, add a defination to csv first line, hash_key:data_type:options If a column's hash_key is empty, we will ignore this column If a column's data_type is empty, :string will be use. options is a JSON of hash, it is optional.

For examples

  • name: equal to name:string
  • , :, :string: this column will be ignore
  • tags::{"range": [2,3]}: we'll put line[2...(2+3)] to the tags key
  • name:undefined_type: if you give a undefined type, and you didn't define it in your code, its type is string
name:bookname,price:float,"tags::{""range"": [2, 3]}",,
World,$ 3.20,B,C,What’s this?
My book,USD 4.3,C,123,
Your book,1.2,Hehe,Haha,666

Define our data type and parser

# `bookname` type was used in that CSV file
# We can define this type, it will tell parser how to parse data of bookname
ext_types = {bookname: proc {|v| "<#{v}>" }}
ArrayHasher.csv_each('path/to/test.csv', ext_types) do |line|
  puts line

# {:name=>"<Hello>", :price=>1.2, :tags=>["A", "B", "C"]}
# {:name=>"<World>", :price=>3.2, :tags=>["B", "C", "What’s this?"]}
# {:name=>"<My book>", :price=>4.3, :tags=>["C", "123", nil]}
# {:name=>"<Your book>", :price=>1.2, :tags=>["Hehe", "Haha", "666"]}

ArrayHasher.csv_each('path/to/test.csv', ext_types) # <Enumerator: xxx>

Put multiple columns to one key.

# We can append a `range` option as third arguments
# `range` also can be used in CSV title
format = ArrayHasher.parse_formatter([
  'attrs:arr:{"range": [2, 100]}'
# => [[:name, :string], [:price, :float], [:attrs, :arr, range: 2..-1]]



See Here

Default Types

  • int # convert string to int
  • float # convert string to float
  • string: # to_s
  • time # Time.parse(string)
  • date # Date.parse(string)
  • json # JSON.parse(string)
  • Proc # format the value with your proc. we can define a Proc in our code only.


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at[USERNAME]/array_hasher.


The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.