Skip to content

Instantly share code, notes, and snippets.

View mlomnicki's full-sized avatar

Michał Łomnicki mlomnicki

View GitHub Profile
@mlomnicki
mlomnicki / json_ostruct.rb
Created January 28, 2016 14:50
JSON + OpenStruct
require 'json'
require 'ostruct'
data = '{"a": {"b": 1}}'
result = JSON.parse(data, object_class: OpenStruct)
result.a.b # => 1
@mlomnicki
mlomnicki / compare_without_order.rb
Last active December 19, 2015 09:39
Check if two arrays have exactly the same elements (we don't care about order)
# Check if two arrays have exactly the same elements (we don't care about order)
keys = [2,1]
1. keys.sort == [1, 2]
2. Set.new(keys) == [1,2].to_set
3. (keys - [1,2]).empty?
4. (keys - [1,2,]).size == 0
@mlomnicki
mlomnicki / delegate.rb
Created October 17, 2012 11:33
ruby 1.8 + SimpleDelegator. extremly slow
require 'delegate'
require 'benchmark'
class User
end
class UserPresenter < SimpleDelegator
end
N = 10_000
@mlomnicki
mlomnicki / client.rb
Created March 3, 2012 15:15
DCell GC
require 'dcell'
DCell.start :id => "client-node", :addr => "tcp://127.0.0.1:2032"
blog = DCell::Global[:blog]
post = blog.new_post
sleep 3
post.title = "hello dcell"
blog.publish_post(post)
@mlomnicki
mlomnicki / app.js
Created December 19, 2011 14:23
ember.js
// Ember.js - some concerns
//a) tightly coupling
App.NewGameView = Em.View.extend({
click: function() {
App.lobbyController.newGameClicked();
}
})
@mlomnicki
mlomnicki / cpistrano_rvm.rb
Created April 20, 2011 14:22
deploy.rb with rvm support
require 'bundler/capistrano'
set :application, "multicomm"
set :repository, "git@XXXX"
set :scm, :git
set :user, 'multicomm'
set :deploy_to, '/var/lib/multicomm'
set :use_sudo, false
set :git_enable_submodules, 1
@mlomnicki
mlomnicki / gist:890485
Created March 28, 2011 13:48
node.js simple benchmark
var pg = require('pg');
var http = require('http');
var conString = "tcp://node:[email protected]/postgres";
var client = new pg.Client(conString);
client.connect();
http.createServer(function(req, res) {
@mlomnicki
mlomnicki / extend_and_method_call_bench.rb
Created February 12, 2011 03:14
Extend + method call VS method call
require 'benchmark'
module Foo
def bar
end
def baz
end
@mlomnicki
mlomnicki / include_vs_extend.rb
Created February 7, 2011 23:48
include_vs_extend
module Foo
def bar
end
def baz
end
@mlomnicki
mlomnicki / splat.rb
Created January 21, 2011 18:39
splat_tricks
a = *"Hello" #=> ["Hello"]