Skip to content

Instantly share code, notes, and snippets.

View banister's full-sized avatar
🇳🇱

John Mair banister

🇳🇱
View GitHub Profile
# Ruby edge cases
# lambdas can no longer be used with instance_eval in Ruby 1.9.2
l = lambda { puts "hello world"! }
Object.instance_eval(&l) #=> invalid argument. This is due to the fact instance_eval now yields a value (the receiver) in 1.9
# module double inclusion
# module appear multiple times in inheritance chain
# behaviour of constants in class_eval
# Proc.new can create a proc from an implicit block
# I want this method in ruby-core
def let
yield
end
def fib(i)
let do |n = 1, result = 0|
if i == -1
result
else
class Hello; end
Hello.singleton_class.class #=> Class
# So methods on Hello.singleton_class should be first looked up as instance methods on Class right?
class Class
def hello
puts "from Class"
end
@lsegal
lsegal / method_ext.rb
Created December 20, 2010 04:55
Method class extensions to introspect YARD docs
require 'yard'
class Method
def docstring
@yard_cache ||= {}
file = source_location[0]
unless @yard_cache[file]
YARD.parse(file)
@yard_cache[file] = true
end
#include <stddef.h>
#include <stdio.h>
typedef struct mystruct {
int x, y;
double f;
char * hello;
} mystruct;
void
direc = File.dirname(__FILE__)
require "#{direc}/local_eval/version"
require 'remix'
require 'object2module'
module LocalEval
module ObjectExtensions
@@m = Mutex.new
def local_eval(*objs, &block)
@banister
banister / traits in ruby.rb
Created November 2, 2010 12:47
exploiting the fact that a method aliasing itself adds the method onto the m_tbl of the receiver
# Traits in Ruby:
module M
def self.included(c)
c.class_eval do
instance_methods.each do |m|
alias_method m, m
end
end
c.uninclude(self)
@banister
banister / ruby moose.rb
Created November 2, 2010 01:25
Imitating Moose-like functionality for Ruby: as seen here http://www.perlmonks.org/?node_id=656019
### Defining some Moose-like functionality
class Module
def has(options)
options.each_pair do |k, v|
singleton_class.send(:attr_accessor, k)
self.send("#{k}=", v)
end
end
def after(meth, &after_block)
@banister
banister / moose using a mixin.rb
Created November 1, 2010 23:15
Perl moose examples in Ruby
# Perl Moose example in Ruby, Perl code found here: http://www.perlmonks.org/?node_id=656019
class Module
# has and after do not exist in Ruby OOTB, so we define them here:
def has(options)
options.each_pair do |k, v|
singleton_class.send(:attr_accessor, k)
instance_variable_set("@#{k}", v)
end
end
@osdezwart
osdezwart / kode.rb
Created October 31, 2010 12:06
kode.rb Autoindenting of ruby code (Kode::Indenter)
=begin
/***************************************************************************
* Copyright (C) 2006, Paul Lutus *
* Copyright (C) 2008, Antono Vasiljev *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *