Skip to content

Instantly share code, notes, and snippets.

@asaaki
Created March 28, 2014 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asaaki/9834918 to your computer and use it in GitHub Desktop.
Save asaaki/9834918 to your computer and use it in GitHub Desktop.
Golfing
# abuse of `inject` and `tap`
#
# it's not really short
# but shows some nice Ruby stuff
%w[bigdecimal /util].inject(""){|a,e|(a<<e).tap{|r|require r}}
# binary to hex string - and back to binary
sig = "<bin data>"
hexsig = sig.unpack("H*").first #=> "3c62696e20646174613e"
sig2 = hexsig.scan(/../).map{|hx| Integer("0x#{hx}").chr}.join
sig2 == sig #=> true
# can be useful in some cases:
(1..23).each_slice(9).with_index.map{|c,i|[i,c]}
#=> [ [0,[1..9]], [1,[10..19]], [2,[20..23]] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment