Skip to content

Instantly share code, notes, and snippets.

@neilconway
Created September 16, 2011 04:02
Show Gist options
  • Save neilconway/1221171 to your computer and use it in GitHub Desktop.
Save neilconway/1221171 to your computer and use it in GitHub Desktop.
instance_exec + blocks
class X
def self.morph(name, &blk)
define_method(name) do |*args, &user_blk|
instance_exec(*args, &blk)
end
end
morph :xyz do |a, b, &blk|
puts "HELLO, WORLD!"
blk.call(a, b)
end
end
X.new.xyz(5, 10) {|a, b| puts "YAY! a = #{a}; b = #{b}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment