Y
Yohanes Santoso
Rando Christensen said:The macro version has made the code much less clear in my mind, which is
precisely the argument against macros in ruby.
Actually, the example Jesse gave was probably bad. A macro, like any
other language construct, can be created to enhance clarity or
obfucate. Thus, one should not make a hasty judgement based on that
one example.
How about this example (which I hope is a better example of
easy-with-macro,-hard-(impossible?)-without):
Suppose we want to do self-documenting methods (not source file),
similar to docstring in emacs:
An implementation with macro would look something like:
def foo(arg)
"This method will return <code>arg</code>"
arg
end
Sure, you can implement this without macro (trivial implementation):
DOCSTRINGS={}
def def_with_docstring(klazz, method_name, docstring, arg_spec, body)
id = "#{klazz.to_s}##{method_name}"
DOCSTRINGS[id] = docstring
klazz.module_eval(%{def #{method_name}(#{arg_spec})
#{body}
end}, __FILE__, 9999) # wish that there isn't
# anything on line 9999.
end
def_with_docstring(Object, "foo", "returns arg", "arg", "arg")
Object.new.foo("hi") # => "hi"
Yes, I am aware you can do more sophisticated implementation so you
can make the above nicer. Yet, as nice as it will be, it won't be as
simple as the example with macro; there will be quirkiness caused by
the language syntax.
For example, rake has this in its documentation: "NOTE: Because of a
quirk in Ruby syntax, parenthesis are required on rule when the first
argument is a regular expression."
Another example: I am not even sure you can turn the above example to
take lambdas as the body. Being able to take lambdas would be nice as
you can take advantage of your editor's syntax highlighting and
auto-indent.
YS.