Skip to content

Instantly share code, notes, and snippets.

View iwan's full-sized avatar

Iwan Buetti iwan

  • Milano, Italy
View GitHub Profile
@iwan
iwan / net_http.rb
Last active December 29, 2015 14:46 — forked from a-chernykh/net_http.rb
require 'net/http'
def download_net_http(uris, thread_count)
queue = Queue.new
uris.map { |uri| queue << uri }
threads = thread_count.times.map do
Thread.new do
while !queue.empty? && uri = queue.pop
@iwan
iwan / activities.js.coffee
Last active September 18, 2015 13:50
Re-sort table rows using buttons/links and jquery on Ruby on Rails
$(document).ready ->
$("a.sortable").on "click", (e) ->
e.preventDefault()
id = $(this).data('id')
dir = $(this).data('direction') # "up" or "down"
sel = $("#item_#{id}") # selector of current
$.ajax
url: "/activities/#{id}/move"
@iwan
iwan / rspec_w_and_wo_cleaning.md
Last active August 29, 2015 14:25
Spec results with and without database cleaning

Spec

require 'rails_helper'

describe "Foo", type: :model do
  it "ex3" do
    puts "--ex3--"
    puts "rep: #{@rep.inspect}"
    puts "Report count: #{Report.count}"
@iwan
iwan / alone.rb
Last active August 29, 2015 14:24
Convert a string numeric array to numeri array
module StringToArrayConverter
NUMERIC_ARRAY_REGEX = /^\[([\d,\s\.\-\+]+)\]$/
def to_numeric_array
m = NUMERIC_ARRAY_REGEX.match(self.strip)
m[1].split(",").map{|e| e.include?(".") ? e.to_f : e.to_i}
end
def is_a_numeric_array?
!NUMERIC_ARRAY_REGEX.match(self.strip).nil?
@iwan
iwan / git.css
Last active August 29, 2015 14:11 — forked from neilgee/git.css
/* Set up Git Configuration */
git config --global user.email "you@yourdomain.com"
git config --global user.name "Your Name"
git config --global core.editor "vi"
git config --global color.ui true
@iwan
iwan / README.md
Last active March 31, 2016 15:18
Drawing histogram based on table row values

Visualize the magnitude of table row values

The goal is to visualize the magnitude of table row values using histogram bars.

Languages and libraries:

  • html + css
  • coffescript / javascript
  • d3 library

Features:

@iwan
iwan / gist:50deff48f6d4163260b8
Last active August 29, 2015 14:04
Template for Rails concern file
module Zo
extend ActiveSupport::Concern
included do
# ...
end
def an_instance_method
# ...
end
@iwan
iwan / route.rb
Last active August 29, 2015 13:59 — forked from vparihar01/route.rb
# NOTE below gems are only for development purpose can be removed and commented out as per requirement
group :development do
gem 'rails-erd','1.0.0'
gem 'hirb','0.7.0'
gem 'railroady'
gem 'itslog','0.6.1'
gem 'quiet_assets'
gem 'sextant' # now included in Rails 4.0
end
FIXME:
WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8
or
ERROR -: Incompatible library version: nokogiri.bundle requires version 11.0.0 or later, but libxml2.2.dylib provides version 10.0.0
gem uninstall nokogiri libxml-ruby
brew update
brew uninstall libxml2
@iwan
iwan / sort_with
Last active August 29, 2015 13:57
Sorting two arrays
# Given two 'linked' array, you want to sort one and reorder the second to preserve the same sequence of the first:
a = [3,2,7,5,1,9,3]
b = [:a, :b, :c, :d, :e, :f, :g]
def a.sort_with(arr)
raise "the two array must have the same size" if size!=arr.size
h = self.collect.with_index{|e,i| [e, arr[i]]}.sort{|x,y| y.first<=>x.first}
[h.map{|e| e.first}, h.map{|e| e.last}]
end