Skip to content

Instantly share code, notes, and snippets.

View json_ostruct.rb
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 Dec 19, 2015
Check if two arrays have exactly the same elements (we don't care about order)
View compare_without_order.rb
# 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 Oct 17, 2012
ruby 1.8 + SimpleDelegator. extremly slow
View delegate.rb
require 'delegate'
require 'benchmark'
class User
end
class UserPresenter < SimpleDelegator
end
N = 10_000
View client.rb
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)
View app.js
// Ember.js - some concerns
//a) tightly coupling
App.NewGameView = Em.View.extend({
click: function() {
App.lobbyController.newGameClicked();
}
})
@mlomnicki
mlomnicki / cpistrano_rvm.rb
Created Apr 20, 2011
deploy.rb with rvm support
View cpistrano_rvm.rb
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 Mar 28, 2011
node.js simple benchmark
View gist:890485
var pg = require('pg');
var http = require('http');
var conString = "tcp://node:node@127.0.0.1/postgres";
var client = new pg.Client(conString);
client.connect();
http.createServer(function(req, res) {
@mlomnicki
mlomnicki / extend_and_method_call_bench.rb
Created Feb 12, 2011
Extend + method call VS method call
View extend_and_method_call_bench.rb
require 'benchmark'
module Foo
def bar
end
def baz
end
View include_vs_extend.rb
module Foo
def bar
end
def baz
end
View splat.rb
a = *"Hello" #=> ["Hello"]