Why is it important to learn about metaprogramming and eigenclasses in Ruby?
Asked Answered
E

2

8

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?
Ephrayim answered 23/8, 2011 at 7:17 Comment(0)
G
17
  • 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 like person.read_attribute("age"), where person is an active record object and the people table has a column called age) 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.

Grefe answered 23/8, 2011 at 7:38 Comment(2)
+1 I really like the attr_accessor example, as it shows important benefit of strong metaprogramming capabilities: metaprogramming keeps the language itself simple. attr_accessor could easily be a separate language construct in another language (related: properties in C# etc.). In Ruby, it is merely another method.Safekeeping
Good answer. While it's true that eigenclass and singleton class mean the same thing in Ruby, the latter is now the preferred term, as indicated by the existence of the methods singleton_class, singleton_methods and define_singleton_method.Mitchiner
B
5

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.

Burnish answered 23/8, 2011 at 23:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.