I am currently experimenting with Ruby and Rails, and I've hit a few sections in tutorials and books about metaprogramming. Many mention that it is an essential component of Ruby but they don't really go into detail. It's as if metaprogramming is the final frontier for Ruby programmers. Coming from a .NET background I am struggling to understand why it is supposedly so useful.
-
开发者_开发知识库
- What benefits are gained when using metaprogramming?
- What is an eigenclass and how is it different from a singleton?
- In what situations is using metaprogramming common?
- What ethical implications are there around using code to modify the behaviour of other code, especially code which is not your own?
What benefits are gained when using metaprogramming?
You can create more expressive APIs than without it (for example ActiveRecord uses metaprogramming to define accessor methods based on a table's column names, so you can write things like
person.age
instead of something likeperson.read_attribute("age")
, whereperson
is an active record object and thepeople
table has a column calledage
) and you can accomplish some things with significantly less code than you otherwise would.What is an eigenclass and how is it different from a singleton?
The terms "eigenclass" and "singleton class" are used interchangeably in the context of ruby.
In what situations is using metaprogramming common?
In situations where you'd otherwise have a lot of boiler plate code or when creating DSLs.
Example of use case 1:
Instead of writing something boiler-plate code like this:
class Foo def bar @bar end def bar=(b) @bar = b end def baz @baz end def baz=(b) @baz = b end end
You can write this much shorter code using the metaprogramming method
attr_accessor
, which automatically defines getter and setter methods with names based on the arguments you give it:class Foo attr_accessor :foo, :bar end
If
attr_accessor
didn't already exist in the standard library, you could define it yourself like this (to give you an idea what metaprogramming in ruby looks like):class Module def attr_accessor(*variable_names) variable_names.each do |variable_name| define_method( variable_name ) do instance_variable_get( "@#{ variable_name }" ) end define_method( "#{ variable_name }=" ) do |value| instance_variable_set( "@#{ variable_name }", value) end end end
end
What ethical implications are there around using code to modify the behaviour of other code, especially code which is not your own?
None.
There's a time and place for metaprogramming. I think it gets mentioned heavily in Ruby books because people like to show off what Ruby can do that other languages can't do as well.
Metaprogramming is like not knowing the Japanese word for "burger" or "noodle soup", but knowing the Japanese for "this one please" ("kore o kudasai"), and being able to point at the item on the menu. It allows more flexibility, but you need more context to know exactly what is being done.
If you're creating ActiveRecord, which allows you to do find_by_foo
, then metaprogramming makes sense.
If you're writing a mutation testing library such as zombie-chaser, or a characterization testing application that tests different implementations of Ruby, such as the Small Eigen Collider, then metaprogramming makes sense.
However, if you're writing an application, then you generally shouldn't be metaprogramming, but merely programming. For example, if you're using instance_variable_set
in your application, that's more a code smell than an indication of how proficient you are.
Related questions you may want to read include https://stackoverflow.com/questions/1236191/what-will-i-learn-from-metaprogramming-ruby and Ruby metaprogramming online tutorial and Metaprogramming how much is too much? .
I really recommend the book "Metaprogramming Ruby", because it doesn't just teach you metaprogramming, but how Ruby works.
精彩评论