Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / reports_controller.rb
Created January 11, 2011 20:13
Example reports controller for Ryan
module MagicShow
def show
method_name = params[:id]
if respond_to? method_name
send(method_name)
else
render :text => "That operation is not supported!", :layout => false
end
end
@hopsoft
hopsoft / pattern_dispatch.rb
Created January 12, 2011 19:34
Pattern Dispatch - Illustrates how to invoke methods based on a convention or pattern
# setup a contrived class to demonstrate pattern dispatch
class Person
attr_accessor :first_name
attr_accessor :last_name
attr_accessor :pets_name
attr_accessor :mothers_maiden_name
def drag_queen_name
"#{pets_name} #{mothers_maiden_name}"
end
@hopsoft
hopsoft / ghost_method.rb
Created January 12, 2011 23:53
Ghost Method - Illustrates how to use method_missing to support methods that aren't defined
# define our example class
class Example
# catch all calls to methods that don't exist
def method_missing(method_name, *args)
puts "You called '#{method_name}' with these arguments: #{args.inspect}"
end
end
# invoke methods that haven't been defined
Example.new.some_method # => You called 'some_method' with these arguments: []
@hopsoft
hopsoft / dynamic_proxy.rb
Created January 13, 2011 00:17
Dynamic Proxy - Illustrates how to wrap and then forward method calls to another object
class Proxy
def initialize(object)
@object = object
end
# forward all calls to the wrapped object
def method_missing(method_name, *args)
@object.send(method_name, *args)
rescue
puts "#{method_name} is not supported by the wrapped object!"
@hopsoft
hopsoft / dynamic_method.rb
Created January 12, 2011 23:39
Dynamic Method - Illustrates how to define methods dynamically at runtime
# setup some data that will drive what methods get defined
$method_names = [:hello, :goodbye]
# define our example class
class Example
# define some dynamic methods
$method_names.each do |method_name|
define_method(method_name) do |name|
puts "#{method_name} #{name}!"
end
@hopsoft
hopsoft / blank_slate.rb
Created January 13, 2011 14:03
Blank Slate - Illustrates how to remove functionality from a class to create a blank slate
# demonstrate how to remove functionality
String.class_eval do
undef_method :length
end
"test".length # => NoMethodError: undefined method `length' for "test":String
# create a blank slate class
class BlankSlate
public_instance_methods.each do |method_name|
undef_method(method_name) unless method_name =~ /^__|^(public_methods|method_missing|respond_to\?)$/
@hopsoft
hopsoft / gcd.rb
Created January 13, 2011 21:55
Testing the GCD system in MacRuby
def async_example(arg, delay=1.0)
Dispatch::Queue.new('org.macruby.examples.gcd').async do
start = Time.now
sleep delay
puts "finished async_work for: #{arg} in #{(Time.now - start).round} secs"
end
end
async_example(:first, 5.0)
async_example(:second, 4.0)
@hopsoft
hopsoft / scope_gate.rb
Created January 14, 2011 05:19
Scope Gate - Illustrates the three ways to define a new scope
# demonstrate scoping in ruby
scope = "global scope"
puts(scope) # => global scope
class ExampleClass
# the globally scoped variable isn't defined in the classes' scope
defined?(scope) # => nil
scope = "class scope"
puts(scope) # => class scope
end
@hopsoft
hopsoft / gist:1122080
Created August 3, 2011 07:14
Rails Environment Based Behavior
# app/models/user.rb
class User < ActiveRecord::Base
end
# lib/user_logger.rb
module UserLogger
def self.included(mod)
mod.send :after_create, :log_creation
end
@hopsoft
hopsoft / monkey_patcher.rb
Created November 29, 2011 19:02
An attempt to define a standard for applying monkey patches
# An attempt to define a standard interface for monkey patching existing method definitions
# on existing Object instances, Classes, and Modules.
#
# This effort warrants a new monkey patching nomenclature.
# * Monkey Patch - a re-definition of an existing method that was patched via MonkeyPatcher
# * Patch - a re-definition of an existing method
#
# Lets get started with some usage examples.
# First lets add some helper methods to all objects.
#