Skip to content

Instantly share code, notes, and snippets.

View gist:d3ff215e88334dd7542a
event = fn
{:startElement, _, 'Contents', _, _}, _, state ->
put_in(state[:current], %{})
{:startElement, _, _name, _, _}, _, state ->
put_in(state[:value], "")
{:characters, data}, _, state ->
update_in(state[:value], &(to_string(data) <> &1))
{:endElement, _, 'Contents', _}, _, state ->
View gist:632cb0e63ad314c34521
if Rails::VERSION::MAJOR > 4
raise "This patch is obsolete and should be removed"
class Time
def compare_with_coercion(other)
# we're avoiding Time#to_datetime and Time#to_time because they're expensive
if other.class == Time
elsif other.is_a?(Time)
View bench.rb
require 'benchmark/ips'
require 'set'
set = Set[:$or, :$and, :$nor]
array = %i($or $and $nor)
Benchmark.ips do |x|
x.config(:time => 10, :warmup => 2)
fcheung / binding.rb
Created Jun 2, 2015
binding from inside c method
View binding.rb
gem 'RubyInline'
require 'inline'
class Test
inline do |builder|
builder.include "<time.h>"
builder.c_raw <<-SRC, :arity => 1
VALUE dummy(VALUE self, VALUE arg){
VALUE ret = rb_funcall(self, rb_intern("binding"), 0);
return ret;
View gist:42383
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 83043c2..8c2507e 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -93,7 +93,11 @@ module ActiveRecord
(class << self; self end).instance_eval do
define_method name do |*args|
- scopes[name].call(self, *args)
+ if scoped? :find
View cl1.rb
class Image
attr_accessor :data
def initialize (data)
@data = data
def output
@data.each do |sub|
sub.each do |cell|
print cell
View challenge#2.rb
class Image
attr_accessor :data
def initialize (data)
@data = data
def image_blur
changes = []
@data.each_with_index do |row, row_index|
fcheung / gist:4072448
Created Nov 14, 2012
BritRuby proposal
View gist:4072448

Going Native

We all love ruby, but sometimes ruby is not enough. Whether it be a performance bottleneck, a killer library written in C or some platform specific functionality you just have to have, sometimes you need to drop down a level.

There have been many talks that show how to take the first steps in writing a ruby C extension. This isn't one of them. I propose instead to give an overview of different ways of extending ruby and show what each approach brings to the table. I intend to cover 'classic' C extensions, RubyInline and FFI across a range of ruby implementations.

About me

View tr_tree.rb
class Tree
attr_accessor :payload, :children
def initialize(payload, children = [])
@payload = payload
@children = children
def traverse(&block)
yield self
@children.each {|child| child.traverse(&block)}

Keybase proof

I hereby claim:

  • I am fcheung on github.
  • I am frederickcheung ( on keybase.
  • I have a public key whose fingerprint is 9A1B 5755 1E45 1BAE 41D4 8905 C4CD 3D1E B871 B631

To claim this, I am signing this object: