Skip to content

Instantly share code, notes, and snippets.

View hopsoft's full-sized avatar

Nate Hopkins hopsoft

View GitHub Profile
@hopsoft
hopsoft / open_classes.rb
Created January 11, 2011 05:25
Open Classes - Illustrates how to re-open a class in Ruby
# define original class
class Example
def say_hello
puts "hello"
end
end
# re-open the class
@hopsoft
hopsoft / monkeypatch.rb
Created January 11, 2011 06:17
Monkeypatch - Illustrates how to re-open a class and redefine existing functionality
# define original class
class Example
def say_hello
puts "hello"
end
end
# re-open the class and monkeypatch some of its existing functionality
@hopsoft
hopsoft / namespace.rb
Created January 11, 2011 06:31
Namespace - Illustrates how to use modules for namespacing
# create a namespace to avoid naming collisions
module Example
class String
def length
100
end
end
end
@hopsoft
hopsoft / kernel_method.rb
Created January 11, 2011 16:12
Kernel Method - Illustrates how to add functionality to all objects
# add a kernel method to make it available to all objects
# this example also serves to illustrate that everything in Ruby is an object
module Kernel
def say_hello
puts "hello from #{self.class.name}"
end
end
@hopsoft
hopsoft / dynamic_dispatch.rb
Created January 11, 2011 18:05
Dynamic Dispatch - Illustrates how to invoke methods that are unknown at design time
# add methods that provide the ability
# to dynamically call unknown methods on objects
def invoke(object, method_name)
object.send(method_name)
end
def invoke_with_args(object, method_name, *args)
object.send(method_name, *args)
end
@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 / 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 / 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!"