Skip to content

Instantly share code, notes, and snippets.

View remigijusj's full-sized avatar

Remigijus Jodelis remigijusj

View GitHub Profile
@remigijusj
remigijusj / monoids_of_size_three.rb
Created July 21, 2020 17:10
Counting all monoids with three elements
class Table
# 1 is an identity, so we know it's composition with other elements.
ELTS = [1, :a, :b].freeze
BASIC = {'11' => 1, '1a' => :a, '1b' => :b, 'a1' => :a, 'b1' => :b}.freeze
# Iterate through all possible composition tables.
def self.each
return to_enum unless block_given?
ELTS.product(ELTS, ELTS, ELTS) { |list| yield new(*list) }
@remigijusj
remigijusj / where_is.rb
Created November 24, 2016 13:54 — forked from wtaysom/where_is.rb
A little Ruby module for finding the source location where class and methods are defined.
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))
@remigijusj
remigijusj / .gitconfig
Created April 18, 2016 08:23
My GIT aliases
[alias]
s = !git status
k = !gitk
br = !git branch
co = "!f() { git checkout `git locax $1`; }; f"
ff = !git reset --hard origin/`git this`
ffm = !git push . origin/master:master
new = !git checkout -b
brd = "!f() { git branch -D `git loca $1`; }; f"
grab = !git fetch origin -p
class Hash
def using(&block)
values = block.parameters.map do |(type, name)|
self[name]
end
block.call(*values)
end
end
@remigijusj
remigijusj / nested_benchmark.rb
Last active August 29, 2015 14:23
comparison of deep data structures
require 'benchmark'
require 'date'
# from lib/core_ext/hash.rb
class Hash
def at(*path)
path.inject(self) do |ob, key|
ob.respond_to?(:each) ? ob[key] : nil
end
end
@remigijusj
remigijusj / gist:af81ab6fccbcd33e94b9
Created June 1, 2015 08:53
KonservativeDK daina apie Lietuva
STOP den lille kænguru
Tre fra Litauen på indbrud
Gemte sig bag ved en hæk
Tævede gamle fru Jensen
Tog pengene med og var væk!
Men så råbte vi:
Stop de østkriminelle
Før de røver igen
module Databound
class Manager
def find_scoped_records(only_extra_scopes: false)
if only_extra_scopes
rel = model.where(@scope.to_h)
else
rel = model.where(params.to_h)
check_permit!(:read, rel) # <<< this is out of place, because SRP!
end
# a monkey-patch fix for OR-query building, needed for Rails 4.2
module Databound
class Manager
def or_query(root, *scopes)
relations = scopes.map do |scope|
root.where(scope.to_h)
end
nodes = relations.map do |rel|
rel.where_values.reduce(:and)
class Array
def deep_compact
term = []
hash = each_with_object({}) do |obj,res|
if obj.is_a?(Hash)
obj.each do |key,val|
res[key] ||= []
res[key] += val.respond_to?(:deep_compact) ? val.deep_compact : val
end
else
@remigijusj
remigijusj / gist:6937669
Created October 11, 2013 16:18
method_missing and splat arguments
class A
def method_missing(_, value, *_)
puts "A: #{value.inspect} #{_.inspect}"
end
def existing1(_, value, *_)
puts "Ae1: #{value.inspect} #{_.inspect}"
end
def existing2(_, value, *)
puts "Ae2: #{value.inspect} #{_.inspect}"
end