Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save baroquebobcat/1719593 to your computer and use it in GitHub Desktop.

Select an option

Save baroquebobcat/1719593 to your computer and use it in GitHub Desktop.
Mirah Macro Ideas
macro def add_puts_to_init str_node
puts_tree = quote do
puts `str_node`
end
init_method = caller.containing_class.method :initialize
unless init_method
caller.containing_class.body << quote do
def initialize
end
end
init_method = caller.containing_class.method :initialize
end
init_method.body << puts_tree
end
# as a macro
macro def alias new_name, old_name
quote do
def `method_name new_name`
`method_call old_name`
end
end
end
# or with a hypothetical define_method that takes a quote block
macro def alias new_name, old_name
define_method new_name do
`method_call old_name`
end
end
macro def eachify list, block
quote do
list.each `block`
end
end
eachify [1,2] do |i|
puts i
end
# macro w/ block that takes args
# modify style
macro def eachify_n_puts list, block
block.body << quote do
puts `block.parameters.first`
end
quote do
list.each `block`
end
end
# macro w/ block that takes args
# insertion style
macro def eachify_n_puts list, block
quote do
list.each do |`block.parameters`|
puts `block.parameters.first`
`block.body`
end
end
end
# macro w/ block that takes args
# "call" style
macro def eachify_n_puts list, block
item = new_param_node # hand waving
quote do
list.each do |`item`|
puts item
`block.call item` # more hand waving
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment