Skip to content

Instantly share code, notes, and snippets.

@fcheung
fcheung / gist:d3ff215e88334dd7542a
Created October 28, 2014 10:32
xmerl elixir snippet
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 ->
if Rails::VERSION::MAJOR > 4
raise "This patch is obsolete and should be removed"
end
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
compare_without_coercion(other)
elsif other.is_a?(Time)
@fcheung
fcheung / bench.rb
Created May 26, 2015 14:37
set vs hash
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
fcheung / binding.rb
Created June 2, 2015 21:09
binding from inside c method
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;
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
end
(class << self; self end).instance_eval do
define_method name do |*args|
- scopes[name].call(self, *args)
+ if scoped? :find
@fcheung
fcheung / cl1.rb
Created September 17, 2015 19:53 — forked from gagaception/challenge#1.rb
cl1.rb
class Image
attr_accessor :data
def initialize (data)
@data = data
end
def output
@data.each do |sub|
sub.each do |cell|
print cell
class Image
attr_accessor :data
def initialize (data)
@data = data
end
def image_blur
changes = []
@data.each_with_index do |row, row_index|
@fcheung
fcheung / gist:4072448
Created November 14, 2012 14:32
BritRuby proposal

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

@fcheung
fcheung / tr_tree.rb
Created October 15, 2015 19:52 — forked from gagaception/tr_tree.rb
tr_tree
class Tree
attr_accessor :payload, :children
def initialize(payload, children = [])
@payload = payload
@children = children
end
def traverse(&block)
yield self
@children.each {|child| child.traverse(&block)}

Keybase proof

I hereby claim:

  • I am fcheung on github.
  • I am frederickcheung (https://keybase.io/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: