This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run with reia bad.re | |
# It will output: | |
# exception: {exception, | |
# {throw, | |
# {tuple, | |
# {error, | |
# {tuple, | |
# {17, | |
# {list,{[],"syntax error before: '|'"}}}}}}}} | |
# stacktrace: [{'Eval',string,1}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InClass | |
def dothis | |
[1,2,3].each do |n| | |
Local.puts(n.to_s()) | |
InClass.start().dothis() | |
puts("lala") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
OlderNewer