I now have to demonstrate my experience in programming, and I think that
doing some Ruby metaprogramming might be sufficient (I finished Why’s
Poignant Guide). I now have to find a project that would involve this.
The problem is, I can’t think of an actually realistic application of
metaclasses. What should I make to show off metaprogramming? I’m open to
suggestions. Thanks!
I now have to find a project that would involve this.
The problem is, I can’t think of an actually realistic application of
metaclasses.
LOL. You have stumbled upon a common conundrum. You have a shiny new
tool that you are eager to use, but there is no reason to use it:)
What should I make to show off metaprogramming? I’m open to
suggestions.
Find some structured data, a file or, if you’re feeling adventurous, a
web API. Then as you are parsing the data construct object
representations of the data with methods to access it.
This is a common use case for meta programming and covers a lot of the
techniques.
You could also make a GUI for “graphical programming”, which lets you
create and use classes and objects in a diagram (like BlueJ for Java).
This is something that I would find interesting, because it actually
makes sense and forces you to do everything with metaprogramming. And
the reviewer has something to play with.
However, this could be a bit too complex, if you have never done
anything with a GUI library.
By the way: Metaclasses have nothing to do with metaprogramming. I
wouldn’t even use the word, because it’s ambiguous (I’d say “eigenclass”
or “singleton class” instead – the latter is actually the “offical”
term used in the built-in methods of Ruby).
Find some structured data, a file or, if you’re feeling adventurous, a
web API. Then as you are parsing the data construct object
representations of the data with methods to access it.
Why would each piece of data require its own method? I don’t see the
purpose of this.
You could also make a GUI for “graphical programming”, which lets you
create and use classes and objects in a diagram (like BlueJ for Java).
This sounds interesting, but I don’t have any experience with a GUI
library except Pygame, which isn’t suitable for the job. I might do
something like this as a personal project later, but I don’t have the
time now.
Create your own version of the Struct class. Here’s a spec for you: Challenge to create your own struct · GitHub
From this spec, I’m not really sure what it’s supposed to do. Structs
are just containers of data, correct?
Find some structured data, a file or, if you’re feeling adventurous, a
web API. Then as you are parsing the data construct object
representations of the data with methods to access it.
Why would each piece of data require its own method? I don’t see the
purpose of this.
Objects have accessor methods for the data they contain.
Create your own version of the Struct class. Here’s a spec for you: Challenge to create your own struct · GitHub
From this spec, I’m not really sure what it’s supposed to do. Structs
are just containers of data, correct?
Yes, that’s all they are. Implementing Struct is an interesting
exercise,
though.
I don’t say that “metaprogramming” has no valid use cases. For example,
I tend to use a config object for my projects, which allows one to call
any method on it, and that object tries to make sense of it when
possible.
But that is really nothing “meta” I can see here.
Meta is the new Agile - buzzwords with no values that will die soon.
Honestly, metaprogramming is just programming. It’s consistent with what
you do everywhere else in Ruby. The real metaprogramming is anything
that
involves a keyword. When you think about what that stuff does, it’s
really
crazy. But since we’re accustomed to it, we don’t notice so much.
I don’t think metaprogramming adds that much overhead. Things like
method_missing and const_missing are the ones that add actual overhead
for
me, because they’re invisible when reflecting on the object. But
defining
methods doesn’t seem crazy to me. The struct example I provided doesn’t
seem complex, you’re just asking for a class with a specific set of
methods, and a known initializer and set of behaviours.
class_eval and instance_eval can also add complexity as things that seem
like benign refactorings can break due to unexpected method lookups.
Honestly, using modules to create large inheritance structures seems
much
more problematic to me. When the entire world is loaded into your
environment, you can’t think about one thing without thinking about
everything. The overhead associated with this is tremendous, and for me
greatly exceeds any pain I’ve felt from dynamically adding / responding
to
methods.
The first thing is to laugh into the face of anyone who is
“metaprogramming”.
Metaprogramming is a fashion run that is hopefully soon dying out.
Sorry, but this is pretty ignorant. Every time you call “attr_accessor”,
you’re doing metaprogramming (or rather reflection). The same goes for
things like “respond_to?”, which is essential to the Ruby type system.
So metaprogramming/reflection isn’t “just a fashion”. It’s a core
feature of Ruby and one of the reasons why Ruby is a lot less verbose
then, say, Java.
Another practical use case of actual metaprogramming is ORM (wrapping
SQL databases in objects) or generating HTML with Ruby syntax.
The first thing is to laugh into the face of anyone who is
“metaprogramming”.
I have to disagree with this. The whole of Rails is built around
metaprogramming. The reading of a database schema and creation of
methods
at runtime, the use of method_missing throughout. Without those things,
Rails would be a totally different framework.
I agree that a lot of the time there are simple ways to do things, but
good
metaprogramming makes beautiful, powerful and DRY code.
For the OP, I’d recommend reading Search and then just using
the techniques in the applications that you build. If you want to build
something specific then the Struct challenge mentioned previously looks
good fun.
On Sat, May 12, 2012 at 05:02:38AM +0900, Phil S. wrote:
I now have to demonstrate my experience in programming
[…]
What should I make to show off metaprogramming?
Don’t show off, it won’t pay off but well may get you in trouble.
See e.g. this nice story of looking at the various “demos”:
It’s like higher math: either you grok it or you can pretend to
but it will be discovered by the very first competent examiner.
Metaprogramming is like equation transforms to make them brief
and clear: if you do have a habit to refactor and de-duplicate
your code, you’re likely to eventually just come to it naturally;
otherwise it won’t stick.
When one quickly prototypes two similar things with copypaste
and then looks at the tested code to seperate the common from
the different (e.g. to form a function which would do both
things depending on its arguments), one’s doing metaprogramming
already. When one writes a program which generates another
program (like automake), it’s that too.
From this spec, I’m not really sure what it’s supposed to do. Structs
are just containers of data, correct?
Yes, that’s all they are. Implementing Struct is an interesting
exercise, though.
But then how are structs different from arrays, if they are used to
store heterogeneous data? Why is there a need to replicate a data type
if it is already implemented in a language?
Arrays are generic, with methods for dealing with collections. Structs
are
custom data structures to fit your specific type of data.
User = Struct.new :first_name, :last_name
user = User.new “Josh”, “Cheek”
user.first_name # => “Josh”
user.last_name # => “Cheek”
Sorry that I’m still not clear on this, but how are they then different
from classes? I know you sort of explained this on the spec, but it’s
not making much sense to me.
not making much sense to me.
They’re not different to classes. Struct gives you a quick and easy way
to create class like containers without having to define accessor
methods. If you need a simple container you could use a Hash except hash
uses [] for accessors so any code that expects an object won’t like it.
An instance of Struct behaves like an Object.
Struct creates the accessor methods for you at initialisation time which
why implementing Struct is a good exercise for meta programming.
I’m getting the impression you need to spend a bit more time with Ruby
before you are going to be ready to tackle meta programming.