Skip to content

Instantly share code, notes, and snippets.

View banister's full-sized avatar
🇳🇱

John Mair banister

🇳🇱
View GitHub Profile
# http://gist.github.com/16885
# $ ./script/console
# Loading development environment (Rails 2.1.0)
# >>
# >> User.whatis :create!
# File: /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/validations.rb:876
# 872: end
# 873:
# 874: # Creates an object just like Base.create but calls save! instead of save
module YCombinator
def y_comb(&generator)
->(x){
->(*args){
generator.(x.(x)).(*args)
}
}.(->(x){
->(*args){
generator.(x.(x)).(*args)
}
typedef struct {
VALUE *pc; /* cfp[0] */
VALUE *sp; /* cfp[1] */
VALUE *bp; /* cfp[2] */
rb_iseq_t *iseq; /* cfp[3] */
VALUE flag; /* cfp[4] */
VALUE self; /* cfp[5] / block[0] */
VALUE klass; /* cfp[6] / block[1] */
VALUE *lfp; /* cfp[7] / block[2] */
VALUE *dfp; /* cfp[8] / block[3] */
@myobie
myobie / scope.rb
Created September 28, 2010 01:25
module Scope
module ScopeMethods
def helpers(&block)
instance_eval(&block)
end
end
def scope(path, &block)
(@path_parts ||= []) << path
module Recipe
def eggs(num=nil)
num ? @eggs = num : @eggs
end
def vinegar(num=nil)
num ? @vinegar = num : @vinegar
end
end
VALUE
rb_singleton_class_clone(VALUE obj)
{
VALUE klass = RBASIC(obj)->klass;
if (!FL_TEST(klass, FL_SINGLETON))
return klass;
else {
struct clone_method_data data;
/* copy singleton(unnamed) class */
@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. *
* *
@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
@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 / 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)