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 original class | |
class Example | |
def say_hello | |
puts "hello" | |
end | |
end | |
# re-open the class |
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 original class | |
class Example | |
def say_hello | |
puts "hello" | |
end | |
end | |
# re-open the class and monkeypatch some of its existing functionality |
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
# create a namespace to avoid naming collisions | |
module Example | |
class String | |
def length | |
100 | |
end | |
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
# 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 |
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
# 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 |
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
# 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
# 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!" |
OlderNewer