cocos (code commons) - auto-include quick-starter prelude & prolog

Intro - Why?

Reason No. 1

After starting of too many scripts (hundreds?) with adding more and always repeating the same dozen modules with require e.g.:

require 'pp'
require 'time'
require 'date'
require 'json'
require 'yaml'
require 'base64'
require 'fileutils'

require 'uri'
require 'net/http'
require 'net/https'

...

why not use a more "inclusive" prelude & prolog and replace the above with a one-liner:

require 'cocos' # auto-include code commons quick-starter prelude & prolog

Reason No. 2

After reading too many text files in utf-8 and always repeating the same open / read and code block dance e.g.:

txt = File.read( "history.txt" )
# sorry - will NOT guarantee unicode utf8-encoding
# (e.g. on microsoft windows it is ISO Code Page (CP-1252
# or something - depending on your locale/culture/language)

txt = File.open( "history.txt", "r:utf-8" ) do |f|
             f.read
         end

Or after reading and parsing too many json files (by default always required utf-8 encoding) and always repeating the same open / read and code block dance again and again e.g.:

txt  = File.open( "history.json", "r:utf-8" ) do |f|
             f.read
         end
data = JSON.parse( txt )

Why not use read convenience / short-cut helpers such as:

txt  = read_txt( "history.txt" )
data = read_json( "history.json" )

And so on.

Usage

Read / Parse

Read / parse convenience short-cut helpers

read_blob( path )
also known as read_binary or read_bin

read_text( path )
also known as read_txt

read_lines( path )

read_json( path ) / parse_json( str )

read_yaml( path ) / parse_yaml( str )

read_csv( path, headers: true ) / parse_csv( str, headers: true )

note: comma-separated values (.csv) reading & parsing service brought to you by the csvreader library / gem »

read_data( path ) / parse_data( str )

note: alternate shortcut / alias for read_csv( path, headers: false ) / parse_csv( str, headers: false )

read_tab( path ) / parse_tab( str )

note: tabulator (\t)-separated values (.tab) reading & parsing service brought to you by the tabreader library / gem »

read_ini( path ) / parse_ini( str )
also known as read_conf / parse_conf

note: ini / conf(ig) reading & parsing service brought to you by the iniparser library / gem »

That's it for now.

License

The cocos scripts are dedicated to the public domain. Use it as you please with no restrictions whatsoever.