Skip to content

Instantly share code, notes, and snippets.

Hopsoft hopsoft

Block or report user

Report or block hopsoft

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@hopsoft
hopsoft / open_classes.rb
Created Jan 11, 2011
Open Classes - Illustrates how to re-open a class in Ruby
View open_classes.rb
# define original class
class Example
def say_hello
puts "hello"
end
end
# re-open the class
@hopsoft
hopsoft / monkeypatch.rb
Created Jan 11, 2011
Monkeypatch - Illustrates how to re-open a class and redefine existing functionality
View monkeypatch.rb
# 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 Jan 11, 2011
Namespace - Illustrates how to use modules for namespacing
View namespace.rb
# create a namespace to avoid naming collisions
module Example
class String
def length
100
end
end
end
@hopsoft
hopsoft / kernel_method.rb
Created Jan 11, 2011
Kernel Method - Illustrates how to add functionality to all objects
View kernel_method.rb
# 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 Jan 11, 2011
Dynamic Dispatch - Illustrates how to invoke methods that are unknown at design time
View dynamic_dispatch.rb
# 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 Jan 11, 2011
Example reports controller for Ryan
View reports_controller.rb
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 Jan 12, 2011
Pattern Dispatch - Illustrates how to invoke methods based on a convention or pattern
View pattern_dispatch.rb
# 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 Jan 12, 2011
Dynamic Method - Illustrates how to define methods dynamically at runtime
View dynamic_method.rb
# 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 Jan 12, 2011
Ghost Method - Illustrates how to use method_missing to support methods that aren't defined
View ghost_method.rb
# 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 Jan 13, 2011
Dynamic Proxy - Illustrates how to wrap and then forward method calls to another object
View dynamic_proxy.rb
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!"
You can’t perform that action at this time.