Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
@ryanlecompte
ryanlecompte / gist:1049170
Created June 27, 2011 16:10
Example of has_many :through
class UserProject < ActiveRecord::Base
belongs_to :user
belongs_to :project
belongs_to :creator, :class => "User"
# add whatever else you want here, this is your join model
end
class User < ActiveRecord::Base
has_many :user_projects
has_many :projects, :through => :user_projects
@ryanlecompte
ryanlecompte / gist:1127872
Created August 5, 2011 16:08
Funky constant lookup change between MRI 1.9.2 vs. 1.9.3
# see this blog post for differences between 1.8.7 vs 1.9.1 vs. 1.9.2: http://jfire.posterous.com/constant-lookup-in-ruby
module M
X = 100
def m1
puts X
end
end
@ryanlecompte
ryanlecompte / gist:1227620
Created September 19, 2011 21:07
Avoiding class variables
module M
@lock = Mutex.new
class << self
attr_reader :lock
end
def lock
M.lock
end
@ryanlecompte
ryanlecompte / gist:1234916
Created September 22, 2011 14:31
Hash#select
ruby-1.9.2-head :001 > h = Hash[*1..10]
=> {1=>2, 3=>4, 5=>6, 7=>8, 9=>10}
ruby-1.9.2-head :002 > h.select {|k,v| k == 5}
=> {5=>6}
ruby-1.9.2-head :003 >
@ryanlecompte
ryanlecompte / gist:1283413
Created October 13, 2011 04:50
Providing an ActiveRecord-like before_filter capability to arbitrary Ruby classes
# First the end result of what we want:
class Foo
before_hook :whoa
before_hook :amazing
def test
puts "This is kinda cool!"
end
ruby-1.9.2-p290 :001 > class A
ruby-1.9.2-p290 :002?> end
=> nil
ruby-1.9.2-p290 :003 > A.ancestors
=> [A, Object, PP::ObjectMixin, Kernel, BasicObject]
ruby-1.9.2-p290 :004 > module M
ruby-1.9.2-p290 :005?> end
=> nil
ruby-1.9.2-p290 :006 > module Kernel
ruby-1.9.2-p290 :007?> include M
@ryanlecompte
ryanlecompte / gist:1371212
Created November 16, 2011 20:12
Ways to find duplicates in an array
ary = [1,1,1,1,2,3,4,5,5,5,5,5,5,10,10,100]
# approach 1
ary.enum_for(:sort).with_object([]) { |(a,b), result| result << a if a == b; a <=> b}.uniq
# approach 2
ary.select { |e| ary.count(e) > 1 }.uniq
# approach 3 (from @apeiros)
ary.group_by {|e| e}.select { |k, v| v.size > 1 }.keys
@ryanlecompte
ryanlecompte / gist:1420133
Created December 1, 2011 21:47
Alternative to modifying Proc directly
# alternative to what is explained in the article Ruby Blocks as Dynamic Callbacks:
# http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
class Twitter
def tweet(msg, &block)
proxy = DSL[block]
publish(msg)
proxy.respond_with(:success)
rescue => e
proxy.respond_with(:failure, e.message)
@ryanlecompte
ryanlecompte / gist:1619490
Created January 16, 2012 06:59
Experimenting with forking and unix sockets in Ruby
require 'base64'
require 'socket'
require 'fileutils'
# UnixSocketForker is an experiment of inter-process communication using
# plain unix sockets to communicate between forked processes and the
# parent process. This can also be done via IO.pipe. In this experiment,
# the jobs are simply random arrays whose sums are calculated in the forked
# worker processes.
class UnixSocketForker
class M
def self.foo
puts Z
end
class Z
end
end
M.foo