Created
February 17, 2010 04:11
-
-
Save expectedbehavior/306283 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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) |
This file contains hidden or 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
# 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) |
This file contains hidden or 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
# 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/} |
This file contains hidden or 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
# 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) |
This file contains hidden or 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
# 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