Skip to content

Instantly share code, notes, and snippets.

View Narnach's full-sized avatar

Wes Oldenbeuving Narnach

View GitHub Profile
@Narnach
Narnach / gist:21324
Created October 31, 2008 15:08
Use a Module to overwrite a method
#!/usr/bin/env ruby
# Goal:
# I want to overwrite an existing method on a class by using a module,
# instead of opening up the class and overwriting the method.
#
# Problem:
# Modules are treated kinda like a superclass.
# This means you can't use it to override a method,
# because it lies further away in the method call chain.
#
# Syntactic sugar for #include? which is used in a lot of container-type classes.
# 1.in? [1,2,3] #=> true
# 2.in? [4,5,6] #=> false
module Kernel
def in?(enum)
enum.include?(self)
end
end
@Narnach
Narnach / bad.re
Created November 30, 2008 18:09
Am I doing something wrong or is this a bug?
# Run with reia bad.re
# It will output:
# exception: {exception,
# {throw,
# {tuple,
# {error,
# {tuple,
# {17,
# {list,{[],"syntax error before: '|'"}}}}}}}}
# stacktrace: [{'Eval',string,1},
class InClass
def dothis
[1,2,3].each do |n|
Local.puts(n.to_s())
InClass.start().dothis()
puts("lala")
@Narnach
Narnach / gist:30702
Created December 1, 2008 10:19
Create a 100x100 thumbnail using ImageMagick
#!/bin/bash
# Creates a 100x100 thumbnail of an image using ImageMagick's convert.
convert -size 300x300 $1 -thumbnail 100x100^ -gravity center -extent 100x100 thumb_$1
@Narnach
Narnach / future.rb
Last active August 16, 2017 10:26
First naive implementation of futures in Ruby
# Simple Future implementation, also known as a Promise in Javascript land.
# Based off an old gist ( https://gist.github.com/Narnach/33661 ) and modernized for Ruby 2.
#
# Wrap a block transparently in a Thread, and redirect any method calls to the `value` of that Thread, aka the return value of a Thread.
# This allows you to create code that runs async but becomes blocking when you try to interact with the result of it.
#
# future = Future.wrap { 1 + 2 }
# future + 3 # => 6
#
# Inherit from BasicObject to not get all the usual cruft and allow _all_ method calls to be proxy'd to the value.
@Narnach
Narnach / gist:37062
Created December 17, 2008 14:28 — forked from samaaron/gist:37031
module A
def hi(*list)
singleton = class << self; self; end
list.each do |l|
singleton.send(:define_method, l) do
puts l
end
end
end
end
@Narnach
Narnach / gist:37074
Created December 17, 2008 14:49
Benchmark to show methods defined using define_method are _slow_
#!/usr/bin/env ruby
#
# Methods defined using Ruby's define_method are slower to invoke than normally defined (eval-ed) methods.
# Using ruby2ruby we can re-define them and get a speedup.
require 'rubygems'
require 'ruby2ruby'
require 'benchmark'
module Bench
@Narnach
Narnach / gist:37538
Created December 18, 2008 15:51 — forked from samaaron/gist:37529
narnach@narnachbook ~/Development $ jruby speed.rb && ruby speed.rb
user system total real
define_method :one 0.107740 0.003617 0.111357 ( 0.156973)
define_method :two 0.075185 0.001356 0.076541 ( 0.098557)
def three 0.033372 0.000635 0.034007 ( 0.042528)
user system total real
define_method :one 0.100000 0.000000 0.100000 ( 0.130217)
define_method :two 0.100000 0.000000 0.100000 ( 0.106536)
def three 0.050000 0.000000 0.050000 ( 0.045840)
@Narnach
Narnach / gist:37544
Created December 18, 2008 16:04 — forked from samaaron/gist:37541
# Ruby 1.8.6p111 (from source) vs ruby1.9.1-preview1_0 (macports) vs jruby (nicksieger's github version cc5d6648b27890c25aa50b3ebb8fb10d30b1df1f)
narnach@narnachbook ~/Development $ ruby speed.rb && ruby1.9 speed.rb && jruby speed.rb
user system total real
define_method :one 0.970000 0.000000 0.970000 ( 0.994811)
define_method :two 0.990000 0.010000 1.000000 ( 1.000196)
def three 0.420000 0.000000 0.420000 ( 0.434437)
user system total real
define_method :one 0.300000 0.000000 0.300000 ( 0.298044)
define_method :two 0.290000 0.000000 0.290000 ( 0.295531)