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 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 |
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
# 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 |
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 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 |
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
require 'yard' | |
class Method | |
def docstring | |
@yard_cache ||= {} | |
file = source_location[0] | |
unless @yard_cache[file] | |
YARD.parse(file) | |
@yard_cache[file] = true | |
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
#include <stddef.h> | |
#include <stdio.h> | |
typedef struct mystruct { | |
int x, y; | |
double f; | |
char * hello; | |
} mystruct; | |
void |
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
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) |
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) |
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
# 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
=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. * | |
* * |