Skip to content

Instantly share code, notes, and snippets.

@expectedbehavior
Created February 17, 2010 04:11
Show Gist options
  • Save expectedbehavior/306283 to your computer and use it in GitHub Desktop.
Save expectedbehavior/306283 to your computer and use it in GitHub Desktop.
# Example #1: proxy div lookup
class ProxyObject
def method_missing(method, *args, &block)
puts "fake send a method to another process: #{method}(*#{args.inspect}, &#{block.inspect})"
end
end
$browser = ProxyObject.new
$browser.send :div, /foo/
#produces:
# fake send a method to another process: div(*[{:text=>/foo/}], &nil)
# Example #2: proxy div & span lookup
class ProxyObject
def method_missing(method, *args, &block)
puts "fake send a method to another process: #{method}(*#{args.inspect}, &#{block.inspect})"
end
end
$browser = ProxyObject.new
$browser.send :div, /foo/
$browser.send :span, /foo/
#produces:
# fake send a method to another process: div(*[{:text=>/foo/}], &nil)
# fake send a method to another process: span(*[{:text=>/foo/}], &nil)
# Example #3: proxied p lookups don't work!
class ProxyObject
def method_missing(method, *args, &block)
puts "fake send a method to another process: #{method}(*#{args.inspect}, &#{block.inspect})"
end
end
$browser = ProxyObject.new
$browser.send :div, /foo/
$browser.send :span, /foo/
$browser.send :p, /foo/
#produces:
# fake send a method to another process: div(*[{:text=>/foo/}], &nil)
# fake send a method to another process: span(*[{:text=>/foo/}], &nil)
# {:text=>/foo/}
# Example #4: eval all the way to the bank
class ProxyObject
def method_missing(method, *args, &block)
puts "fake send a method to another process: #{method}(*#{args.inspect}, &#{block.inspect})"
end
end
$browser = ProxyObject.new
eval "$browser.div /foo/"
eval "$browser.span /foo/"
eval "$browser.p /foo/"
#produces:
# fake send a method to another process: div(*[{:text=>/foo/}], &nil)
# fake send a method to another process: span(*[{:text=>/foo/}], &nil)
# fake send a method to another process: p(*[{:text=>/foo/}], &nil)
# example #5: it handles everything you throw at it
def find_stuff(*args, &block)
%w(lasers hand_grenades p).each do |tag_name|
eval "$browser.#{tag_name} *args, &block"
end
end
find_stuff :text => /foo/
#produces:
# fake send a method to another process: lasers(*[{:text=>/foo/}], &nil)
# fake send a method to another process: hand_grenades(*[{:text=>/foo/}], &nil)
# fake send a method to another process: p(*[{:text=>/foo/}], &nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment