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
# 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 |
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 YCombinator | |
def y_comb(&generator) | |
->(x){ | |
->(*args){ | |
generator.(x.(x)).(*args) | |
} | |
}.(->(x){ | |
->(*args){ | |
generator.(x.(x)).(*args) | |
} |
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
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] */ |
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 Scope | |
module ScopeMethods | |
def helpers(&block) | |
instance_eval(&block) | |
end | |
end | |
def scope(path, &block) | |
(@path_parts ||= []) << path |
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 Recipe | |
def eggs(num=nil) | |
num ? @eggs = num : @eggs | |
end | |
def vinegar(num=nil) | |
num ? @vinegar = num : @vinegar | |
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
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 */ |
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
=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. * | |
* * |
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
# 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 |
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
### 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) |
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
# 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) |
OlderNewer