• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

How is metaprogramming in Ruby better?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aloha,

I have recently picked up an interest in Ruby and I am curious to learn about metaprogramming. What makes it different then regular programming practices?

Thanks.
 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better? It does seem that asking which is better is a bit like you are comparing raspberries and steak. Both are great, useful, and healthy, but have different (and perhaps complimentary) uses. I don't think I'd want to choose one or the other exclusively.
 
author
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's difficult to explain how metaprogramming works and how it's useful in just a few sentences (unless you already know about it, and then it feels very easy and natural).
Instead, I can point you to the Introduction chapter from Metaprogramming Ruby. You can download it freely here: http://media.pragprog.com/titles/ppmetr/introduction.pdf .
Enjoy!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Metaprogramming is programming about programming: it's code that writes code, but in a different way than compile-time generation like most Java code generation. Because Ruby is highly dynamic, things like getters and setters can be written by a method that writes additional methods:This ends up generating a class that looks like this:A trivial example, yes, but it's important to understand when and how this is happening. This allows very simple programmatic generation of instance methods *at runtime*. Now think about something like this:Now, with an absolute minimum of intrusiveness, we've defined some instance requirements, at runtime, in code, using a mini-validation DSL. validates_presence_of and validates_length_of are methods that generate behavior.

One thing I worked on was a test framework for web pages. Driving Selenium, I was able to code and test pages like this:

Like Lisp macros, metaprogramming can be difficult to explain, but the power and clarity it can provide is *huge*.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic