Skip to content

Instantly share code, notes, and snippets.

View lsegal's full-sized avatar

Loren Segal lsegal

View GitHub Profile
@lsegal
lsegal / export_features.rb
Created April 1, 2010 05:41
Eiffel-like export features syntax for fine-grained method visibility
class A; def bar; D.new.foo end end
class B; def bar; D.new.foo end end
class C; def bar; D.new.foo end end
class D
def foo; "HELLO WORLD!" end
# Export `foo` to A and B, but not C
export :foo, A, B
end
require 'yard'
class DefineMethodHandler < YARD::Handlers::Ruby::Base
handles method_call(:define_method)
process { register MethodObject.new(namespace, statement[1].jump(:ident)[0]) }
end
YARD.parse_string(<<-'eof')
class Foo
def self.something
require 'yard'
module YardObject
def source; yard_object.source end
def docstring; yard_object.docstring end
private
def yard_object
fname = source_location[0]
require 'yard'
class IncrementalArray
def initialize(block)
@block = block
@buffer = YARD::Parser::Ruby::Legacy::TokenList.new
end
def shift; fill_buffer; @buffer.shift end
def first; fill_buffer; @buffer.first end
def [](index) fill_buffer; @buffer[index] end
require 'yard'
class IncrementalArray
def initialize(block)
@block = block
@buffer = YARD::Parser::Ruby::Legacy::TokenList.new
end
def shift; fill_buffer; @buffer.shift end
def first; fill_buffer; @buffer.first end
def [](index) fill_buffer; @buffer[index] end
@lsegal
lsegal / snippet.rb
Created April 7, 2010 03:12
Scoped implicit conversion in Ruby, code is http://github.com/lsegal/ruby/tree/implicit
class MyString < String
def foo; reverse + "X" end
end
module Foo
implicit String => MyString
class A
attr_reader :myattr
def initialize
module Callbacks
@@callbacks = {}
def has_callback_hook(name)
@@callbacks[name] = []
class_eval <<-CLASS
def #{name}_add block
@callbacks[#{name}] << block
end
# Array#flatten seems to call #to_ary on all elements of the array, regardless
# of whether they are Arrays and without checking if the object has a to_ary first.
#
# More peculiarly, calling "super" inside of method_missing does not return from
# the call to the method_missing method, instead it jumps completely out of
# the call without performing any actions after "super" is called.
#
# Note that Foo.new.respond_to?(:to_ary) == false, so method_missing should raise an
# exception, though it does not.
require 'tmpdir'
require 'fileutils'
require 'open-uri'
module YARD
module CLI
class Diff < Command
def initialize
super
@list_all = false
yard (0.6-master)$ yard diff -a yard-0.5.2 yard-0.5.3
Added objects:
File.read_binary
Gem::Specification#has_yardoc=
YARD::CLI::Base#initialize
YARD::CLI::Yardoc#excluded
YARD::CLI::Yardoc#excluded=
YARD::CLI::Yardoc#incremental
YARD::CLI::Yardoc#incremental=