Skip to content

Instantly share code, notes, and snippets.

# A naive approach to implement a picky search
module SelectiveSearchable
def searchable_on(*attrs)
search_attrs = attrs.map { |attr| "#{attr}_cont".to_sym }
define_singleton_method(:picky_search) do |query, page = 0, limit = 10|
if query.try(:empty?)
sentence = Proc.new { self.all }
else
q = search_attrs.reduce({}) {|dict, attr| dict.merge({attr => query}) }
@everblut
everblut / puma.rb
Last active August 29, 2015 14:18 — forked from thelinuxlich/puma.rb
threads 3, 3
workers 2
on_worker_boot do
require "active_record"
require "erb"
cwd = File.dirname(__FILE__)+"/.."
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
configuration = YAML.load(ERB.new(File.read("#{cwd}/config/database.yml")).result)
ActiveRecord::Base.establish_connection(configuration[ENV["RAILS_ENV"]])
@everblut
everblut / transposicion.rb
Created November 4, 2013 02:35
Cifrado por transposicón
#!/usr/ruby
# *****------->>>>>Usage: cesar.rb 'texto'
def transposicion(msg)
cif = C(msg).map(&:reverse).transpose.map { |fila| fila.join('') }.join(' ')
return "****---->>>> Mensaje cifrado: #{cif} \n****---->>>> Mensaje descifrado: #{C(cif,cif.split[0].size).reverse.transpose.map { |fila| fila.join('') }.join(' ')}"
end
def C(msg,s=-1)
arr,ran, matrix = [msg.split.join.split('')] + [s == -1 ? Random.rand(3...6) : s] + [[[]]]
ran.times {|n| arr.each_slice(ran).with_index { |bloq,i| matrix[i] = Array.new(ran).each_with_index.map { |val,idx| val = bloq[idx].nil? ? (10...36).map { |i| i.to_s 36 }.sample : bloq[idx] } } }
return matrix
@everblut
everblut / cesar.rb
Created November 4, 2013 01:48
Cifrado cesar
#!/usr/ruby
# *****------->>>>>Usage: cesar.rb 'texto' #Desplazamiento
ALFABETO = ((0...36).map { |i| i.to_s 36 } + " ,/;'[]\=-!@#.$%^&*<>?:{}|".chars) * 10
def cesar(msg,n)
viejo = [nuevo = msg.chars.map { |c| C(TRUE,c,n.to_i) }.join('')][0].chars.map { |c| C(FALSE,c,n.to_i) }.join('')
puts "*****----> El mensaje despues de E(msg,#{n}): #{nuevo} \n*****----> El mensaje despues de D(msg,#{n}): #{viejo}"
end
def C(s,x,n)
return s ? ALFABETO[ (ALFABETO.index(x) + n) % ALFABETO.count] : ALFABETO[ (ALFABETO.index(x) - n) % ALFABETO.count]
end
@everblut
everblut / gist:3533301
Created August 30, 2012 17:00
Chars.py
#!/usr/bin/python2
#################################################################################################################
# TODO #
# tecnicas dict(k:tupla)? (tecnica(costo,funcion) diccionario que saca tupla #
# Validaciones .:. #
# Necesita Indicadores reales y temporales, cuando el hp este bajo el limite. #
# Ejemplo: Vida max = 100, after damage = 80, use potion = efect !> 100 #
# #
#################################################################################################################
#
@everblut
everblut / Python
Created May 24, 2012 16:09
Python controlador musica
import serial
import os
def U():
print 'boton Up volumen++'
os.system("amixer sset Master playback 5%+")
def D():
print 'boton Down volmen--'
os.system("amixer sset Master playback 5%-")
@everblut
everblut / Arduino
Created May 24, 2012 16:06
Control Musica
/*Variables a usar*/
int latch = 2; // set the latch pin
int clock = 3; // set the clock pin
int datin = 4;// set the data in pin
byte datosDelControl = 0;
void setup() {
Serial.begin(9600);
@everblut
everblut / C
Created May 22, 2012 15:32
PIC
#include <16F628A.h> //Pic a utilizar
#FUSES NOWDT //No usar el watchdog
#FUSES INTRC_IO //Usar el cristal interno, sin CLKOUT
#FUSES BROWNOUT //activa el brownout
#use delay(clock=4000000) // frecuencia de 4Mhz
void main( void ){
@everblut
everblut / C
Created May 22, 2012 15:10
PIC
#include <16F628A.h> //Pic a utilizar
#FUSES NOWDT //No usar el watchdog
#FUSES INTRC_IO //Usar el cristal interno, sin CLKOUT
#FUSES BROWNOUT //activa el brownout
#use delay(clock=4000000) // frecuencia de 4Mhz
void main( void ){
@everblut
everblut / gist:2766946
Created May 22, 2012 06:01
Interrupciones
int Interruptor = 0; // El interruptor a utilizar sera el 0 es decir el pin digital 2
int ledSalida = 4; // Aqui pondremos un led para ver como funcionan las interrupciones
int estado = HIGH; // El estado del led
void setup()
{
// ponemos en modo output el led
pinMode(ledSalida, OUTPUT);
digitalWrite(ledSalida, estado); //Lo encendemos