finished encode and decode implementation for ruby 2