This file contains 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
require 'csv' | |
CSV.open("path/to/file.csv", "wb") do |csv| | |
csv << Model.attribute_names | |
Model.find_each do |model| | |
csv << model.attributes.values | |
end | |
end |
This file contains 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
module JsonSerializer | |
module_function | |
# | |
# Usage: | |
# JsonSerializer.find_by_id(User, 1, only: [:id, :email, :address], except: [:address]) | |
# | |
def find_by_id(klass, id, options = {}) | |
sql = "SELECT row_to_json(results) FROM ( | |
SELECT #{selected_attributes(klass, options)} |
This file contains 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
// Example taken from Javascript The Good Parts | |
var memoizer = function(memo, formula) { | |
var recur = function(n) { | |
var result = memo[n]; | |
if (typeof result !== 'number') { | |
result = formula(recur, n); | |
memo[n] = result; | |
} | |
return result; |
This file contains 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
require 'interactor' | |
require 'dry-types' | |
# Author:: Alejandro Bezdjian | |
# Copyright:: Copyright (c) 2017 Alejandro Bezdjian | |
# License:: Apache 2.0 | |
class InteractorMixin | |
include Interactor | |
This file contains 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
require 'pathname' | |
require 'json' | |
################################################################################ | |
# USAGE: # | |
# Execute this script from console like # | |
# $> ruby variable_checker.rb ./repositories_to_check_example.json # | |
# # | |
# Create your own repositories.json file following the example # | |
################################################################################ |
This file contains 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
new_tag_version = ARGV[0] | |
GIT_REPOS = [] | |
class GitRepository | |
GIT_MERGE_COMMITS = /(Merge pull request)|(Merge branch)/ | |
GIT_HASH_LENGTH = 8 | |
attr_reader :name |
This file contains 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
class Spy | |
def self.spy(klass, method) | |
define_counter(klass, method) | |
klass.singleton_class.class_eval do | |
define_method(method) do |*args, &block| | |
instance_variable_set("@spied_#{method}_counter", instance_variable_get("@spied_#{method}_counter") + 1) | |
super(*args, &block) | |
end | |
end | |
end |
This file contains 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
import numpy as np | |
class SimplePerceptron: | |
def __init__(self, input_layer_size, output_layer_size, coeff = 0.05): | |
self._bias = -1 | |
self._coeff = coeff | |
self._input_layer_size = input_layer_size | |
self._output_layer_size = output_layer_size | |
# Weights is a matrix in which each column represents the weights from every input | |
# to each output and has the bias in each column |
This file contains 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
class JSHash | |
def initialize(hash) | |
@original_hash = hash | |
@hash = build_recursively(hash) | |
end | |
def method_missing(m, *args, &block) | |
response = @hash.send(:[], m.to_s, *args, &block) | |
return response if response | |
response = @hash.send(:[], m.to_sym, *args, &block) |
This file contains 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
module Configurable | |
def self.with(*attrs) | |
not_provided = Object.new | |
config_class = Class.new do | |
attrs.each do |attr| | |
define_method attr do |value = not_provided, &block| | |
if value === not_provided && block.nil? | |
result = instance_variable_get("@#{attr}") | |
result.is_a?(Proc) ? instance_eval(&result) : result |
OlderNewer