Skip to content

Instantly share code, notes, and snippets.

iwan / sort_with
Last active Aug 29, 2015
Sorting two arrays
View sort_with
# 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}
[{|e| e.first},{|e| e.last}]
WARNING: Nokogiri was built against LibXML version 2.7.3, but has dynamically loaded 2.7.8
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
View 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
iwan / gist:50deff48f6d4163260b8
Last active Aug 29, 2015
Template for Rails concern file
View gist:50deff48f6d4163260b8
module Zo
extend ActiveSupport::Concern
included do
# ...
def an_instance_method
# ...
iwan / git.css
Last active Aug 29, 2015 — forked from neilgee/git.css
View git.css
/* Set up Git Configuration */
git config --global ""
git config --global "Your Name"
git config --global core.editor "vi"
git config --global color.ui true
iwan / alone.rb
Last active Aug 29, 2015
Convert a string numeric array to numeri array
View alone.rb
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}
def is_a_numeric_array?
iwan /
Last active Aug 29, 2015
Spec results with and without database cleaning


require 'rails_helper'

describe "Foo", type: :model do
  it "ex3" do
    puts "--ex3--"
    puts "rep: #{@rep.inspect}"
    puts "Report count: #{Report.count}"
iwan / iwan.rb
Created Sep 24, 2009
Reading a csv file using FasterCSV
We couldn’t find that file to show.
iwan /
Last active Sep 18, 2015
Re-sort table rows using buttons/links and jquery on Ruby on Rails
$(document).ready ->
$("a.sortable").on "click", (e) ->
id = $(this).data('id')
dir = $(this).data('direction') # "up" or "down"
sel = $("#item_#{id}") # selector of current
url: "/activities/#{id}/move"
iwan / array_to_hash.rb
Last active Dec 23, 2015
Ruby. From Array to Hash
View array_to_hash.rb
# Given an array
arr = [1, 2, 3]
# I want to build an hash starting from an array
# like { 1 => 1, 2 => 4, 3 => 9} (where the values are the square of keys)
# long form:
hash = {}
arr.each do |e|
hash[e] = e**2