This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
->(m, n){->(collection){collection[rand(collection.size)]}.call(("1"*n).to_i(2).up_to(("1"*m).to_i(2)).select{|i| i.to_s(2).gsub('0', '') == "#{'1'*n}"})}.call(m_value, n_value).to_i(2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lambda(m, n) do | |
seed = (rand(2).even?) ? 0b0 : 0b1 | |
num_bit_1_left = (seed == 0b1) ? n-1 : n | |
bits_length = 1 | |
(m-1).times do | |
bit_mask = if num_bit_1_left > 0 | |
((m - bits_length) > num_bit_1_left) ? ((rand(2).even?) ? 0b0 : 0b1) : 0b1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function display_sub_tree(node, _level) | |
{ | |
level = _level || 0 ; | |
result = '<ul class="level-'+level+'">' ; | |
children_of(node).each(function(child) | |
{ | |
result = result + '<li>' + child.name ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# V2, please see https://gist.github.com/FranckyU/9024250 for V1 | |
# UPDATES | |
# - Monkey patching ActiveRecord::Base to to handle it all | |
# - The 'with_..._connection' methods get native without having to include any module | |
# ----------------------------------- | |
# PUT THE FOLLOWING IN AN INITIALIZER | |
# ----------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I have been recently exposed to Martin Fowler's CQRS concept and saw some implementations on .Net | |
# Like this http://www.codeproject.com/Articles/555855/Introduction-to-CQRS | |
# Disliked it as it brings a lot of noise in the code, a lot of handlers, class imbrications that defies simple human logic | |
# To say shortly, it's not quite cool, and as a rubyist I like keeping it simple and human | |
# Though the core concept of CQRS is quite simple, separate writes and reads to different database connections to increase global performance | |
# But implementation is very complex | |
# Lastly, someone I know even spent 2 years on a .Net project using CQRS (don't know how many weeks/months spend for just implementing the CQRS thing) | |
# I wrote this gist to see whether it can be done in 10 minutes | |
# And I did it in 10 minutes | |
# I love ActiveRecord so I DRYed by just extending it |