28

I have just started to learn Ruby and got a good take on the basics. I keep hearing that one of the cool things that Ruby does very well is metaprogramming, but none of the tutorials I've read cover this. Searching Google I can only seem to find paid for ruby metaprogramming screen casts.

So, where can I find a good Ruby metaprogramming tutorial online?

Robin Barnes
  • 13,133
  • 15
  • 44
  • 45

6 Answers6

24

The above article does refer to this but I'll post it for clarity: http://viewsourcecode.org/why/hacking/seeingMetaclassesClearly.html

  • 1
    The Dwemthy's Array sample linked from there is a great (if not entirely practical) example of metaprogramming. – Chuck Apr 26 '09 at 08:00
  • This link seems to be broken :( – Kev Jun 10 '10 at 18:34
  • 1
    This can now be found here: http://dannytatom.github.com/metaid/ Amazing. – Edward Anderson Jun 12 '10 at 16:16
  • a quick intro http://www.alfreddd.com/2013/02/metaprogramming-in-ruby-in-nutshell.html – Alfreddd Feb 21 '13 at 15:11
  • And don't forget http://mislav.uniqpath.com/poignant-guide/ which is Why’s (Poignant) Guide to Ruby – Douglas G. Allen May 25 '13 at 18:32
  • 1
    Although this is the classic example of metaprogramming in Ruby, it is rather outdated. For one it uses _why's made up notation (metaclass, meta_def, meta_eval, etc.) rather than the more recent official terminology (singleton_class, define_singleton_method, etc.) – Max Aug 20 '13 at 15:27
7

If you are prepared to outlay a little cash, then The Pragmatic Programmers website has an excellent screencast series on the Ruby Object Model and metaprogramming. It's not very expensive and is a very comprehensive series that goes over a lot of cool stuff and worth every cent.

I need to watch it again to try and get my head around some of the concepts, then I can be one of the cool meta crowd :)

nitecoder
  • 5,496
  • 1
  • 28
  • 35
  • 2
    excellent PragProg screencast series indeed! http://www.pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming – Tilo May 08 '11 at 04:31
7

This link explains various concepts(singleton,dynamic programming etc) of Ruby metaprogramming in a very systemetic manner.

http://ruby-metaprogramming.rubylearning.com/

vireshas
  • 806
  • 6
  • 19
6

These 2 books should get you going...

"The Ruby Programming Language" by Matz and Flanagan.

"Metaprogramming Ruby: Program Like the Ruby Pros" by Paolo Perrota

user528306
  • 61
  • 1
  • 1
  • +1 for "MetaProgramming Ruby". The book is basically a mentor exposing how these 'tricks'/'spells' are used. Apart from Ruby constructs related to metaprogramming it also shows how these features are USED in Rails to create all that "Rails-Magic"/conventions. – GuruM Apr 06 '14 at 08:11
  • Metaprogramming Ruby Second Edition for Ruby 2.x is in beta April 2014. The Beta ebook is available here : http://pragprog.com/book/ppmetr2/metaprogramming-ruby See also : http://forums.pragprog.com/forums/288/topics/12582 – GuruM Apr 06 '14 at 10:00
  • [Video of Metaprogramming Ruby (1.9) by Dave Thomas](https://www.youtube.com/watch?v=X2sgQ38UDVY) – GuruM Apr 06 '14 at 10:38
2

You can take a look at http://rubymonk.com/learning/books/2-metaprogramming-in-ruby - it's the second free book on programming in Ruby that we've release on RubyMonk.com.

Kai Wren
  • 442
  • 3
  • 6
1

I'm surprised no one has mentioned Code School. Their "Ruby Bits Part 2" course follows material similar to the second half of the excellent book "Eloquent Ruby.", with a nice set of interactive tests to help drill the material into you.

Link: http://www.codeschool.com/courses/ruby-bits-part-2

Levels:

  1. Blocks, Procs, & Lambdas
  2. Dynamic Classes & Methods
  3. Understanding Self
  4. Missing Methods 5 & 6. DSLs

It does cost $20 a month for a subscription (for which you also get access to all of their other courses), but for me it was totally worth it.

Ozzie Gooen
  • 122
  • 6