Skip to content

Instantly share code, notes, and snippets.

View werner's full-sized avatar
🏠
Working from home

Werner Echezuría werner

🏠
Working from home
View GitHub Profile
@werner
werner / habtm_bug.rb
Created July 1, 2014 18:47
Habtm and foreign key Bug
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.2'
#For Heroku
gem 'pg'
@werner
werner / habtm_callbacks.rb
Created July 2, 2014 16:57
Habtm and before create callback
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
Rails as it has never been before :)
Rails 3 AntiPatterns, Useful snippets and Recommended Refactoring.
Note: Some of them are collected from different online resources/posts/blogs with some tweaks.
@werner
werner / shortcuts.rb
Created August 5, 2015 19:54
Ruby on Rails personal shortcuts
#Remove console logs
ActiveRecord::Base.logger.level = 1
#Create a csv file
require "csv"
CSV.open("path/file_to_create.csv", "wb") { |csv|
csv << ["Field 1", "Field 2", "Field 3"]; records.each {|t| csv << [t.field1, t.field2, t.field3.count] }
}
#Remove duplicates
@werner
werner / macro_borrowing_problem.rs
Created July 26, 2017 13:46
Macro borrowing problem
macro_rules! get_body_as {
($structure:ty, $req:expr, $error_fn:ident) => {
{
let body = get_body!($req, $error_fn);
let raw_structure = borrow_function(&body);
match raw_structure {
Ok(structure) => structure,
Err(error) => "Error"
@werner
werner / p2sh_p2wpkh.rb
Last active January 7, 2019 20:56 — forked from pierrenoizat/p2sh_p2wpkh.rb
Generate Segwit P2SH-P2WPKH address, create tx spending from it. with xpub
require 'btcruby'
require 'bitcoin'
require 'bech32'
require './segwit_addr'
require 'active_support'
require 'active_support/core_ext'
require 'ffi'
# Computation of P2SH-P2WPKH address
@werner
werner / decoding.rs
Last active January 8, 2019 20:30
decoding
fn main() {
let spanish_freq = [15.0,12.53, 1.42, 4.68, 5.86, 13.68, 0.69, 1.01, 0.70, 6.25, 0.44, 0.02, 4.97, 3.15, 6.71, 0.31, 8.68, 2.51, 0.88, 6.87, 7.98, 4.63, 3.93, 0.90, 0.01, 0.22, 0.90, 0.52];
let test_paragraph = "ΣΦΨΞΔλΨΔΛΣΦΔλΨξΔϗΞΔΦΨΞϑλΨΛΣΘϑΞϗΦϑλΨΣΞΨλϑΞΨζβΣφΔΨΣΦΨΣΞΨξΛϗΞΞϑΨϖΣΞΨΠΣϖΛΣφΔΞΨΩΨΠΛΣΦϖϗϖϑΨΔΨΞΔΨΘΔφϗΔΨϖΣΨΞϑλΨΓΔΘϗΦϑλΨΣΞΨΔΛΛϗΣΛϑΨαΔΨΣΞΨΔΛΛϗΣΛϑΨαΔΨΣλΨξΔΦϖΣΛΔΨϖΣΨΦϗΣξΞΔΨλβΨΠϑΦΓΡϑΨΔΞΨαϗΣΦμϑΨΞϑΨλΔΞβϖΔΦΨΞΔλΨεΞΔβμΔλΨϖΣΞΨΠΔζϑΦΔΞΨΩΨΔΦϗΘΔΦϖϑΨΞΔΨμΛϑΠΔΨΠΔΛΨΣλϑλΨΓΣΛΛϑλΨΣΞΨΔΛΛϗΣΛϑΨαΔΨΣΞΨΔΛΛϗΣΛϑΨαΔΨΞΔλΨΠΣΦΔλΨΩΨΞΔλΨαΔηβϗμΔλΨλΣΨαΔΦΨΠΔΛΨΞΔΨΘϗλΘΔΨλΣΦϖΔΨΞΔλΨΠΣΦΔλΨλϑΦΨϖΣΨΦϑλϑμΛϑλΨΞΔλΨαΔηβϗμΔλΨλϑΦΨΔζΣΦΔλ";
let char_array = test_paragraph.split("").collect::<Vec<&str>>();
let mut count: Vec<f64> = vec![0.0;381];
let mut result = vec![0;381];
for (i, char) in char_array.iter().enumerate() {
count[i] = (char_array.iter().filter(|&x| x == char).collect::<Vec<&&str>>().len() as f64 * 100.0) / 381.0;
}
@werner
werner / keoken.rb
Last active January 24, 2019 20:15
Keoken ruby
require 'bitcoin'
KEOKEN_PREFIX_SIZE = '04'
KEOKEN_PREFIX = '00004b50'
KEOKEN_VERSION = '0000'
KEOKEN_TYPE_CREATE_ASSET = '0000'
KEOKEN_TYPE_SEND_TOKEN = '0001'
PREFIX_BYTE_AMOUNT = "0000000000000000"
AMOUNT_TOKEN_TO_CREATE = 1000000.to_s(16)
AMOUNT_SIZE = 16
@werner
werner / sh
Last active April 24, 2019 12:41
ctags
bundle show --paths | xargs ctags -R --exclude=.git --exclude=log -f ./.git/tags /home/werner/proyectos/torta
https://github.com/rust-lang/rls/issues/1449#issuecomment-489026206