Skip to content

Instantly share code, notes, and snippets.

@janx
janx / passenger-httpd.spec
Created December 15, 2011 09:09
passenger-httpd.spec
Summary: Rubygem Passenger and its Apache2 module
Name: passenger-httpd
Version: 3.0.11
Release: 1
Vendor: Socialspring, Inc
Group: System Environment/Daemons
License: Modified BSD
URL: http://www.modrails.com/
Source: http://rubyforge.org/frs/download.php/75548/passenger-3.0.11.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot-%(%{__id_u} -n)
@janx
janx / xmonad.hs
Created December 24, 2011 12:00
my xmonad config
import XMonad hiding ( (|||) )
import XMonad.Actions.CopyWindow
import XMonad.Actions.CycleWS
import XMonad.Actions.NoBorders
import XMonad.Actions.Submap
import XMonad.Actions.Search
import XMonad.Actions.WindowBringer (bringWindow)
import XMonad.Config.Gnome
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ManageHelpers
@janx
janx / refactor.patch
Created February 10, 2012 02:01
refactoring of Membership create
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index 5747a13..c5c9a05 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -12,8 +12,7 @@ class Admin::UsersController < AdminController
@email = Email.new(email: @identity.email, confirmed: true)
@user = User.create!(name: @identity.name, nickname: @identity.nickname,
emails: [@email])
- # create the membership for other domain user.
- Membership.create!(:account => account, :email => @email) unless account.membership_for(@user)
@janx
janx / Problem when run 'rake'
Created November 15, 2012 06:22
setup patch
14:23 ~/projects/work/rentals master rake
(in /home/jan/projects/work/rentals)
warning: metric_fu gem desired but not found.
/home/jan/.rvm/gems/ruby-1.8.7-p352/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning: Gem::Dependency#version_requirements is deprecated and will be removed on or after August 2010. Use #requirement
/home/jan/projects/work/rentals/lib/endeca/dimension.rb:3: undefined method `reader' for Endeca::Dimension:Class (NoMethodError)
from /home/jan/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:158:in `require'
from /home/jan/.rvm/gems/ruby-1.8.7-p352/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:158:in `require'
from /home/jan/projects/work/rentals/config/initializers/requires.rb:1
from /home/jan/projects/work/rentals/config/initializers/requires.rb:1:in `each'
from /home/jan/projects/work/rentals/config/initializers/requires.rb:1
@janx
janx / gc_middleware.rb
Created May 2, 2013 11:02
GC trick, reduce gc frequency by time factor, create hiccups in response time
class GCMiddleware
def initialize(app, options={})
@app = app
@options = options
end
def call(env)
begin_gc_deferment
result = @app.call(env)
reconsider_gc_deferment(env)
@janx
janx / hash_patch.rb
Created May 7, 2013 12:52
Monkey patch hash so it behaves like OpenStruct
class Hash
alias :__values :values
def values
self["values"] || __values
end
alias :__method_missing :method_missing
def method_missing(name, *args, &block)
if args.empty?
self[name.to_s] || self[name]
@janx
janx / About.markdown
Last active January 28, 2023 15:36
Memory leak research on OpenStruct and #define_method.

OpenStruct and #define_method

#define_method will create a closure, without careful use it could cause memory leak, as pointed out here.

OpenStruct use #define_method to create accessors dynamically, we suspect that will cause memory leak.

So I create two scripts to prove it, however the result shows the opposite: OpenStruct doesn't leak memory.

open_struct_leak.rb

@janx
janx / object_space_middleware.rb
Last active December 17, 2015 18:59
ObjectSpace.count_objects graph middleware
class ObjectSpaceMiddleware
GRANULITY = 50
@@files = {}
@@count = 0
def self.file(type)
unless @@files[type]
@@files[type] = File.open File.expand_path("../../../tmp/count_#{type}", __FILE__), 'a'
@janx
janx / _virtus_vs_plain_method.markdown
Last active December 18, 2015 03:48
Some thoughts on Virtus use in our project.

The more Virtus code I wrote, the more awkward I feel it is. It's not because Virtus is inherently bad, I just don't think it fits our case.

  1. So many attributes need to be marked reader: :private. It's not ruby's way. Ruby has its own private keyword.

  2. NilToDefault is bad. How do I decide whether a model need to include it? Or we simply include it everywhere?

  3. The difference between writer_class and default is subtle. Why does the implementer add a new writer_class here? Why does he use default value there? We're using different ways to do the same thing (compute values) here.

  4. writer_class is awkward. Why do I need to create a class just for its coerce method? I feel like writing Java.

/home/jan/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems/riak-client-1.0.5/lib/riak/client/net_http_backend.rb:58:in `block (2 levels) in perform': Expected 200 from Riak but received 500. <html><head><title>500 Internal Server Error</title></head><body><h1>Internal Server Error</h1>The server encountered an error while processing this request:<br><pre>{error,{error,badarg, (Riak::HTTPFailedRequest)
[{lists,split,
[-500,
[{<<"apartmentguide_development_search">>,
<<"1373995701">>,
[{p,[0,0]},{score,0.03162748954634035}]},
{<<"apartmentguide_development_search">>,
<<"1373997031">>,
[{p,[0,0]},{score,0.03162748954634035}]},
{<<"apartmentguide_development_search">>,