Skip to content

Instantly share code, notes, and snippets.

@topher6345
topher6345 / power_set.rb
Created July 9, 2016 02:03
Power Set in Ruby
require 'set'
class Set
def power_set
make_permutation = proc { |n| self.to_a.permutation(n).to_a.map(&:to_set).to_set}
Set.new([Set.new([]),(1..self.size).map(&make_permutation)])
end
end
Set.new([1,2,3]).power_set
def traverse instance
attributes = instance.attributes
klass = instance.class
associations = klass.reflect_on_all_associations
return attributes unless associations
associations.map(&:name).each do |key|
result = instance.send key

Apache Kafka

a new messaging-based log aggregator

a distributed messaging system

horizontally scalable messaging system.

Memory Mapped Files

Kernel Space processing

import subprocess
import sublime, sublime_plugin
import re
class EvalAsRuby:
# Method to run Ruby from the shell
def ruby(self):
try:
# Example:
# "sno_phort":
# YADTTMCG
## Yet another Donald Trump Tweet Markov Chain Generator
require 'sinatra'
require 'open-uri'
require 'nokogiri'
def next_word ary
ary[rand(ary.length).to_i][1]
end
# == YADTQMCG
## Yet another Donald Trump Quote Markov Chain Generator
# Markov algorithm forked from
# https://gist.github.com/michaelfeathers/2cf9e1599e06d0563f2e
file = ARGF.read.split
pseudo_stems = file.each_cons(4).to_a.shuffle
pseudo_stems << file.each_cons(3).to_a.shuffle
pseudo_stems << file.each_cons(5).to_a.shuffle
pseudo_stems << file.each_cons(6).to_a.shuffle
@topher6345
topher6345 / lispo.rb
Created March 10, 2016 06:00
A basic 4 operator LISP implemented in ruby.
module LISPO
module ArrayCallable
refine Array do
def call
inject &shift
end
end
end
class Parser
# Replace Keys K of a Hash H with Keys K'
# Where L is an unordered superset of K with possible duplicates
hash = { 'foo-1' => { data: 102 } , 'bar-1' => { data: 109 } , 'baz-1' => { data: 12 } }
array = ['bar-2', 'foo-2', 'baz-2', 'fab-3', 'gib-2', 'gib-2', 'yup-3']
compare = -> o { o[0][/\w+/] != o[1][/\w+/] }
key_map = array.zip(hash.keys.cycle).flatten.sort.each_slice(2).to_a.reject(&compare).to_h
apply_new_key = -> k, v { [key_map[k], v] }
hash.map(&apply_new_key).to_h
@topher6345
topher6345 / reverse_binary_tree.rb
Created February 28, 2016 23:08
REVERSE A BINARY TREE
Node = Struct.new(:left, :right)
Tree = Node.new(
Node.new(
Node.new(
0,
1
),
Node.new(
2,
3
@topher6345
topher6345 / Default (OSX).sublime-keymap
Last active November 5, 2018 20:18
Sublime Text RuboCop autocorrect selected text
[
{ "keys": ["super+shift+c"], "command": "rubo_fix"},
{ "keys": ["super+k", "super+d"], "command": "rubo_fix" }
]