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 / 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
@remigijusj
remigijusj / gist:4538360
Created January 15, 2013 12:39
better helper?
# WHICH IS BETTER HELPER?
# -----------------
%Q{
<div class="invitation-info-box clearfix"><p>
#{invite_friends_link}
#{t('app.invitation.info_box_signed_in')}
</p></div>
}.html_safe
# -----------------
@remigijusj
remigijusj / hot-items-queries.sql
Created October 30, 2012 16:26
some hot items SQL
-- ratio with average view count log-trend
SELECT i.id FROM items i
LEFT JOIN global_stats vc ON vc.entry_type=510 AND vc.entry_id = i.id
INNER JOIN user_settings s ON s.user_id = i.user_id
WHERE s.is_publish_photos_agreed = 1 AND is_visible = 1 AND is_replicated = 0 AND is_reserved = 0
AND boutique_id IS NULL
AND created_at >= '2012-10-27'
AND i.catalog_id IN (%cat4%)
AND vc.value / (LN(DATEDIFF(NOW(),created_at)+1)*15.5+12.4) >= 0.8
ORDER BY id DESC
@remigijusj
remigijusj / gist:3927443
Created October 21, 2012 16:26
GIT pre-commit hook to check .rb and .erb syntax
#!/Ruby192/bin/ruby.exe
#
# A hook script to verify that only syntactically valid ruby code is commited.
# Called by git-commit with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Put this code into a file called "pre-commit" inside your .git/hooks
# directory, and make sure it is executable ("chmod +x .git/hooks/pre-commit")
@remigijusj
remigijusj / gist:1385465
Created November 22, 2011 11:30
ruby-golf
# using Ruby 1.9.2
# Hole 1, 41 chars
def fizzbuzz(n)
"#{n%3<1&&f=:Fizz;n%5<1?"#{f}Buzz":f||n}"
end
# Same as Cyrus's solution, optimized
p fizzbuzz(3) # => "Fizz"
p fizzbuzz(10) # => "Buzz"
@remigijusj
remigijusj / gist:870925
Created March 15, 2011 15:57
dm-redis-adapter with "through" associations
require 'dm-core'
require 'dm-redis-adapter'
DataMapper.setup(:default, {:adapter => "redis"})
class Book
include DataMapper::Resource
property :id, Serial
property :name, String