Skip to content

Instantly share code, notes, and snippets.

module Enumerable
# operates like Enumerable#map, but returns a hash
def hmap
inject Hash.new do |new_hash, (k,v)|
new_k, new_v = yield k, v
new_hash[new_k] = new_v
new_hash
end
end
end
@acook
acook / install_vboxa.sh
Last active December 23, 2015 04:09
Install VirtualBox Additions on Debian / Ubuntu / Crunchbang / ElementaryOS / etc
# If DKMS isn't installed then VBox will erroneously complain about missing headers.
sudo apt-get install build-essential dkms libglib2.0-0 linux-headers-$(uname -r)
# Only needed if the system doesn't automount the image:
#sudo mount /dev/cdrom /media/cdrom
# Some systems automount on `/media`, others on `/media/cdrom*` which is symlinked to `/media/cdrom`.
# Don't `cd` into these directories until you're sure the image is mounted.
cd /media ; cd cdrom
# Need root privs in order to place and activate kernel modules.
sudo sh VBoxLinuxAdditions.run
module Stuff
def an_instance_method
puts "Stuff's instance method!"
end
def self.an_eigenclass_method
puts "Stuff's eigenclass method!"
end
module_function
@acook
acook / BinaryDataHandling.markdown
Last active December 23, 2015 10:29
List of gems useful for parsing/generating binary data from Ruby.

Binary Data

It can be challenging to use Ruby for handling raw binary data, involving a lot of Array#pack and String#unpack with the possibility of String#ord and Numeric#chr for good measure. Some of these tools might make the job a bit easier, allowing your to define schemas of the binary data and how to parse it, or even just allowing your to specify the format in a more verbose way.

Gems

BinData

# starting daemon
$ gnome-keyring-daemon -s --components=pkcs11,secrets,ssh,gpg
gnome-keyring-daemon: insufficient process capabilities, unsecure memory might get used
** Message: couldn't access conrol socket: /home/xybre/.cache/keyring-My42JI/control: No such file or directory
$ git fetch
WARNING: gnome-keyring:: couldn't connect to: /home/xybre/.cache/keyring-My42JI/pkcs11: No such file or directory
$ touch /home/xybre/.cache/keyring-My42JI/control
touch: cannot touch `/home/xybre/.cache/keyring-My42JI/control': No such file or directory
@acook
acook / caller_binding.rb
Last active December 24, 2015 21:29
Allows you to swap the values of local variables with a method. Yep, all this for just that. Actually, you can drop all the caller_binding stuff and it'll work the same, you'll just have to pass in the binding explicitly then.
if defined?(Rubinius) then
require_relative 'rbx_caller_binding'
else
require_relative 'mri_caller_binding'
end
class ServerThing
def initialize session
@session = session # primary parameters
end
def for type, session
self.class.const_get("#{type.to_s.capitalize}Class").new_with self, ServerThing::Session.new(session)
end
class SuperClassForObjects
@acook
acook / README.md
Last active December 24, 2015 23:19 — forked from smileart/README.md

My modified fork of agnoster.zsh-theme

A ZSH theme optimized for people who use:

  • Solarized
  • Git
  • Unicode-compatible fonts and terminals (I use iTerm2 + Menlo)

Compatibility

@acook
acook / hash_dup.rb
Last active December 26, 2015 02:19
my_dict = { foo: 1, bar: 'baz' }
my_clone = my_dict.dup #=> making a shallow copy
my_clone.object_id == my_dict.object_id #=> false, these are different objects
my_clone == my_dict #=> true, they contain the same values
my_clone[:qux] = 'wibble' #=> adding a pair to the clone
my_clone.has_key? :qux #=> true, the key was added
my_dict.has_key? :qux #=> false, the key was never added to this object, neither was its value
Black: 0, 0, 0
Red: 229, 34, 34
Green: 166, 227, 45
Yellow: 252, 149, 30
Blue: 196, 141, 255
Magenta: 250, 37, 115
Cyan: 103, 217, 240
White: 242, 242, 242