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
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 |
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
# 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 |
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
# 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: [] |
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 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!" |
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
# 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 |
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
# 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\?)$/ |
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
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) |
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
# 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 |
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
# 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 |
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
# 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. | |
# |