Skip to content

Instantly share code, notes, and snippets.

View bil-bas's full-sized avatar

Bil Bas bil-bas

View GitHub Profile
require 'mixico'
module Redirector
# @param [Object] target Target of the redirection.
# @param [Array<Symbol>] methods ([]) If empty, redirect all methods, otherwise only redirect named ones.
def self.create(target, *methods)
my_module = Module.new
if methods.empty?
# Equivalent to gen_eval, but won't crash and probably lower performance.
module Fidgit
# Redirects methods to an object, but does not mask methods and ivars from the calling context.
module RedirectorMethods
# Evaluate a block accessing methods and ivars from the calling context, but calling methods (not ivars) on this
# object if they don't exist on the calling context.
def instance_methods_eval(&block)
raise ArgumentEror unless block_given?
context = eval('self', block.binding)
C:/Users/Spooner/RubymineProjects/fidgit/examples/scroll_window_example.rb:44: [BUG] Segmentation fault
ruby 1.9.2dev (2010-05-31) [i386-mingw32]
-- control frame ----------
c:0008 p:---- s:0019 b:0019 l:000018 d:000018 CFUNC :(null)
c:0007 p:---- s:0017 b:0017 l:000016 d:000016 CFUNC :new
c:0006 p:0043 s:0014 b:0014 l:000013 d:000013 TOP C:/Users/Spooner/RubymineProjects/fidgit/examples/scroll_window_example.rb:44
c:0005 p:---- s:0012 b:0012 l:000011 d:000011 FINISH
c:0004 p:---- s:0010 b:0010 l:000009 d:000009 CFUNC :load
c:0003 p:0051 s:0006 b:0006 l:000434 d:0006cc EVAL -e:1
@bil-bas
bil-bas / gosu_char_width_erratic.rb
Created November 10, 2010 16:16
The former fails; the latter works
# encoding: ascii-8bit
# To put ö directly into a string, I need to set the encoding to ascii-8bit.
require 'gosu'
include Gosu
class Game < Window
def initialize
super 640, 480, false
@bil-bas
bil-bas / register_instances.rb
Created February 23, 2011 17:23
tracking subclasses
class Base
def self.register_instances
@@instances ||= {}
@@instances[self] || = []
@@subclass_instances ||= {}
@@subclass_instances[self] || = []
end
register_instances
class Object
class << self
# @example
# before(:initialize) do
# log.debug "Initializing chicken"
# end
def before(method_name, &block)
raise "Requires block" unless block_given?
original = instance_method(method_name)
@bil-bas
bil-bas / banister.rb
Created March 15, 2011 00:18
pry highlighting
process_comment_markup = lambda do |comment, code_type|
if Pry.color
comment.gsub!(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, code_type).term }
{"(?:em|i)" => 32, "(?:strong|b)" => 34}.each_pair do |tag, color|
comment.gsub!(/<#{tag}>(?:\s*\n)?(.*?)\s*<\/#{tag}>/m) { console_colorize($1, color) }
end
{"\\+" => 34, "_" => 32}.each_pair do |tag, color|
comment.gsub!(/\B#{tag}[^\s]+?#{tag}\B/) { console_colorize($1, color) }
end
comment.gsub!(/((?:^[ \t]+.+(?:\n+|\Z)/) { CodeRay.scan($1, code_type).term }
require 'renet'
class Client
attr_reader :id, :server, :ip
def initialize(server, id, ip)
@server, @id, @ip = server, id, ip
end
def send_packet(data, channel=0, reliable=true)
@bil-bas
bil-bas / gist:949175
Created April 29, 2011 22:46
LD 20 themes
Absorb
Aging
Anachronism
Big and Small
Broken Physics
Climbing
Colony
Colossal
Evolution
Girl Games
@bil-bas
bil-bas / gist:976418
Created May 17, 2011 12:56
logging to file and output
workmad3: Spooner: tbh, if you did pretty much exactly the same thing as I just did, but grabbed the stdout reference, and opened the file into the two ivars, it would work
[12:46] banisterfiend: sweet_horse = SweetHorse.new
[12:46] workmad3: banisterfiend: woo! infinite recursion!
[12:46] Spooner: Yeah workmad3's answer seems more sensible :)
[12:46] banisterfiend: workmad3: STDOUT.puts blah
[12:46] banisterfiend: ;)
[12:46] workmad3: ;)
[12:47] Spooner: Thanks. I'll probably do that then :)
[12:47] injekt: pretty sure stdout depends on a write method, banisterfiend
[12:47] workmad3: Spooner: banister's method is exactly the same as mine, but creates the puts method explicitly rather than hijacking method_missing