I’d like to write this:
[:p, :h1, :h3].each do |tag|
define_method(tag) { |text| "<#{tag}>#{text}</#{tag}>" }
end
It’s just some simple methods to wrap text in HTML tags. I want to be able to use these methods in the rest of the script. Unfortunately the define_method method seems to only work inside of a module. But if I did this inside a module, I wouldn’t be able to cleanly write p "This is a paragraph.", it’d be something like HTML::p "This is a paragraph." which would be pretty terrible.
So how do I define methods like this globally?
I don’t know your whole situation, but you probably don’t really want to be defining global methods. If you don’t want to type the
HTML::then add aninclude HTMLstatement at the beginning of your code.