Created
January 21, 2016 20:55
-
-
Save anonymous/ee06bb192d546e4cea1e to your computer and use it in GitHub Desktop.
Refactoring a block for use with ansible provisioning
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
#!/usr/bin/env ruby | |
class TestClass | |
def initialize(str) | |
@class_str = str | |
end | |
# This is the method we want to call | |
def foo(str, &block) | |
# We're going to pass the STDOUT object into the block. This was ansible before. | |
STDOUT.puts "You passed in '#{str}', and object contains '#{@class_str}'" | |
block.call(STDOUT) | |
end | |
end | |
test = TestClass.new "hello" | |
# This is a standin for the original ansible code that we want to replace | |
test.foo "original" do |io| | |
io.puts "...and this is being printed inside the original-style block" | |
# ... more complicated stuff here | |
end | |
# But we want to write, with the same effect, something like bar(foo) | |
def bar(method) | |
# We pass in the method and explicitly call it | |
# with the same block syntax after it | |
method.call("refactor") do |io| | |
io.puts "...and this is being printed inside the refactored block" | |
# ... more complicated stuff here | |
end | |
end | |
# So now we can pass in the method object and it's called for us | |
# We just need to get the actual method we want to called | |
# Note that the instance it's called on is preserved | |
bar(test.method(:foo)) | |
# So the ansible version would be | |
# add_ansible_provisioner(override.vm.method(:provision)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment