Skip to content

Instantly share code, notes, and snippets.

@richo
Created October 30, 2012 09:37
Show Gist options
  • Save richo/3979268 to your computer and use it in GitHub Desktop.
Save richo/3979268 to your computer and use it in GitHub Desktop.
self.class.send(:define_method, :"has space") do |arg|
puts "lol" << arg
end
self.class.send(:define_method, :"has several spaces") do |arg|
puts "Ahahah. Lol." << arg
end
self.class.send(:define_method, :"takes several args") do |arg, arg2|
puts "tuuuuup" << arg << arg2
end
self.instance_exec do
def method_missing(sym, *args)
# Splat args if passed in from a parent call
if args.length == 1 && args[0].is_a?(Array) && args[0][0].class == NameError
args = args[0]
end
method_names, arguments = args.partition { |a| a.class == NameError }
method([sym.to_s, *method_names.map(&:name)].join(" ")).call(*arguments)
rescue NameError => e
return [e, *arguments]
end
end
has space "rawr"
has several spaces "seriously"
takes several args "like", "this"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment