Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created June 27, 2015 00:06
Show Gist options
  • Save domgetter/52ae28bc89e8d487d6c0 to your computer and use it in GitHub Desktop.
Save domgetter/52ae28bc89e8d487d6c0 to your computer and use it in GitHub Desktop.
Examples of using blocks to achieve Inversion of Control in Ruby
puts "hello"
# refactor to a method call
def hello
puts "hello"
end
hello
#refactor to invert control
def hello
yield
end
hello do
puts "hello"
end
#refactor to invert control
def hello
yield "hello"
end
hello do |phrase|
puts phrase
end
#refactor to invert control
def hello
puts yield
end
hello do
"hello"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment