Best practices for Ruby Meta-programming

Discussion in 'Ruby' started by harshal, Mar 6, 2008.

  1. harshal

    harshal Guest

    Dear all,

    Ruby meta-programming is a real fun.
    However no power comes without responsibility, so does the Ruby's meta-
    programming.
    Can anybody share the experiences with meta-programming (for example,
    in writing DSLs.) so that if possible we can come up with best
    practices?
    It would be great if somebody is already aware of such best practices.

    Thanks.

    Warm regards,
    Harshal
     
    harshal, Mar 6, 2008
    #1
    1. Advertising

  2. harshal

    Bil Kleb Guest

    Hi,

    harshal wrote:
    >
    > Can anybody share the experiences with meta-programming (for example,
    > in writing DSLs.) so that if possible we can come up with best
    > practices?


    Martin Fowler is crafting a book along these lines:

    http://martinfowler.com/dslwip/

    Regards,
    --
    Bil Kleb
    http://twitter.com/bil_kleb
     
    Bil Kleb, Mar 6, 2008
    #2
    1. Advertising

  3. harshal

    James Britt Guest

    harshal wrote:
    > Dear all,
    >
    > Ruby meta-programming is a real fun.
    > However no power comes without responsibility, so does the Ruby's meta-
    > programming.
    > Can anybody share the experiences with meta-programming (for example,
    > in writing DSLs.) so that if possible we can come up with best
    > practices?


    Don't be evil.

    Don't be stupid.


    I think that about covers it.


    --
    James Britt

    www.risingtidesoftware.com - Wicked Cool Coding
    www.jamesbritt.com - Playing with Better Toys
     
    James Britt, Mar 6, 2008
    #3
  4. harshal

    Avdi Grimm Guest

    On Thu, Mar 6, 2008 at 1:15 PM, James Britt <> wrote:
    > Don't be evil.
    >
    > Don't be stupid.


    So THAT's what I've been doing wrong all this time. Wish someone had
    clued me in sooner.

    My concrete contribution to this discussion:

    Modules are your friend. When tempted to dynamically extend a class
    with new methods, consider adding them to a module instead
    (potentially a dynamically created module using Module.new). Then use
    #include or #extend to add the module to your target. This has a
    number of benefits, including:

    * Clients can clearly see which which methods come from your
    extension vs. the methods that are part of the class itself, by using
    Ruby's reflection.
    * Clients can override dynamically created methods and still reuse
    them with #super() - no need to resort to aliasing.
    * A module can be re-used for either class-wide extension or
    extending a single object.

    --
    Avdi
     
    Avdi Grimm, Mar 6, 2008
    #4
  5. harshal

    harshal Guest

    That's absolutely true.

    It reminds me Einstein's quote "Make it simple, no simpler" (or
    similar).

    I think 'Module'-related suggestion is a good beginning.
    Some of the best practices would still be valid from traditional OOAD.

    However, the place where Ruby differentiates itself from traditional
    programming languages is - Meta-programming.
    So I am expecting few 'new patterns' just because of meta-programming.
    I could not get any chance to look at the book 'Design patterns in
    Ruby'.

    Thanks.

    Harshal

    On Mar 7, 12:19 am, Avdi Grimm <> wrote:
    > On Thu, Mar 6, 2008 at 1:15 PM, James Britt <> wrote:
    > > Don't be evil.

    >
    > > Don't be stupid.

    >
    > So THAT's what I've been doing wrong all this time. Wish someone had
    > clued me in sooner.
    >
    > My concrete contribution to this discussion:
    >
    > Modules are your friend. When tempted to dynamically extend a class
    > with new methods, consider adding them to a module instead
    > (potentially a dynamically created module using Module.new). Then use
    > #include or #extend to add the module to your target. This has a
    > number of benefits, including:
    >
    > * Clients can clearly see which which methods come from your
    > extension vs. the methods that are part of the class itself, by using
    > Ruby's reflection.
    > * Clients can override dynamically created methods and still reuse
    > them with #super() - no need to resort to aliasing.
    > * A module can be re-used for either class-wide extension or
    > extending a single object.
    >
    > --
    > Avdi
     
    harshal, Mar 7, 2008
    #5
  6. harshal

    Avdi Grimm Guest

    harshal wrote:
    > However, the place where Ruby differentiates itself from traditional
    > programming languages is - Meta-programming.


    If Ruby differentiates itself it's in the form of a particularly
    expressive syntax, or maybe in the way it brings an amalgam of different
    language features together. Metaprogramming itself is nothing new.
    Heck, I used to do metaprogramming in C++.

    --
    Avdi
     
    Avdi Grimm, Mar 7, 2008
    #6
  7. Avdi Grimm wrote:
    > harshal wrote:
    >> However, the place where Ruby differentiates itself from traditional
    >> programming languages is - Meta-programming.

    >
    > If Ruby differentiates itself it's in the form of a particularly
    > expressive syntax, or maybe in the way it brings an amalgam of different
    > language features together. Metaprogramming itself is nothing new.
    > Heck, I used to do metaprogramming in C++.
    >
    > --
    > Avdi
    >
    >

    Hell ... I've done metaprogramming in macro assembler and FORTRAN. ;)
     
    M. Edward (Ed) Borasky, Mar 7, 2008
    #7
  8. Metaprogramming. Isn't that just another name for self-modifying code.
    Only had to do that once in a game to get past a hardware limitation on
    frame-flyback time for a display driver. (Had to split the screen into
    1/3rds to get the maximum number of hardware sprites to treble :) ) That
    was really the only time I *had* to do it and could justify it
    convincingly since we were down to counting t-states.

    <FX:Ducks behind sofa and sobs quietly>
    --
    Posted via http://www.ruby-forum.com/.
     
    Paul Mckibbin, Mar 11, 2008
    #8
  9. harshal

    Robert Dober Guest

    On Tue, Mar 11, 2008 at 2:25 AM, Paul Mckibbin <> wrote:
    > Metaprogramming. Isn't that just another name for self-modifying code.
    > Only had to do that once in a game to get past a hardware limitation on
    > frame-flyback time for a display driver. (Had to split the screen into
    > 1/3rds to get the maximum number of hardware sprites to treble :) ) That
    > was really the only time I *had* to do it and could justify it
    > convincingly since we were down to counting t-states.
    >
    > <FX:Ducks behind sofa and sobs quietly>

    Duck is good, if you really go to the extremes of Ducktyping you need
    metaprogramming in Ruby. Modules and Classes just wont do, hopefully I
    will have time soon to elaborate on this.
    Cheers
    Robert
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Mar 11, 2008
    #9
  10. > programming (for example,
    > in writing DSLs.) so that if possible we can come up with best
    > practices?
    > It would be great if somebody is already aware of such best practices.


    Don't overuse method_missing, it can be conflictive with other
    frameworks and dsl in your application.


    --
    -------------------------------------
    Pedro Del Gallego

    Email :
     
    Pedro Del Gallego, Mar 11, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karim
    Replies:
    0
    Views:
    481
    karim
    Jul 13, 2003
  2. swille

    programming best practices

    swille, Nov 7, 2005, in forum: Ruby
    Replies:
    15
    Views:
    205
    Stu Glaser
    Nov 12, 2005
  3. Erik Veenstra

    Meta-Meta-Programming

    Erik Veenstra, Feb 7, 2006, in forum: Ruby
    Replies:
    29
    Views:
    428
    Erik Veenstra
    Feb 8, 2006
  4. Erik Veenstra

    Meta-Meta-Programming, revisited

    Erik Veenstra, Jul 21, 2006, in forum: Ruby
    Replies:
    21
    Views:
    476
    Erik Veenstra
    Jul 25, 2006
  5. Replies:
    16
    Views:
    909
    Arne Vajhøj
    Jul 23, 2012
Loading...

Share This Page