views:

114

answers:

5

Hi. I'm at the point in learning Ruby where I'd like to look at some small-ish libraries' source code to see how they were built. I do not know what is considered a small library but was hoping SO could recommend some easy-to-understand libraries to study.

So if anyone knows a very tiny library or two that's a good example for novice Rubyists to study, please recommend!

I wanted to use Manveru's Innate lib because it tries to stay under 2000 LOC but I'm not yet familiar with the Ruby shorthand that is used so much within it.

Maybe some ~100 - ~500 LOC libs would be better for me? Thank you.

+1  A: 

You could check out Github. There are many Ruby projects from "very small" up to "way too much".

daddz
Thanks for the advice. I assume GitHub is where I'll find what I'm looking for but without specific recommendations it's hard to know if the library "works" or is just a testbed or whatever.
lol, I just posted the same xD
knoopx
+3  A: 

Some picks from github.com:

knoopx
Awesome! (and here's some additional characters to meet the minimum comment requirement.)
+1  A: 
  • sinatra is an about 1.5K library is quite readable
  • the first version of rake had 78 lines of code, according to the author - maybe there is some older version around ..

  • much more on github.com ...

The MYYN
Thank you for this. Wow, 78 lines? Hard to believe that could do so much!
Wow, Sinatra is much more readable than I thought it would be!
+3  A: 

Ryan "ZenSpider" Davis's stuff is always interesting. It is however, not entirely suited for beginners. On the one hand, Ryan is absolutely fanatical about keeping things, simple, short and clean, which helps understandability quite a bit. On the other hand, he does use pretty much all of Ruby's power to achieve that.

One of the most amazing examples of his work, is MiniTest, the new testing framework which replaces Test::Unit in the Ruby 1.9 standard library. It contains a full xUnit style testing framework in 419 lines, an RSpec style BDD framework in 87 lines and a full mocking framework in a whopping 28 lines.

But, for example, the way that the BDD framework is so small, is that it uses reflective metaprogramming to dynamically loop over the assertion methods from the testing framework and rename them to BDD style must_ methods.

Another nice example is also a BDD framework by Christian Neukirchen: Bacon has 291 lines. Also check out his other stuff, for example his mocking framework in 131 lines, forum software in 488 lines and blog engine in 146 lines.

Last but not least, there is Gregory Brown's Prawn PDF Generation library. The interesting thing about Prawn is actually that it was deliberately written not only as a PDF Generation library but also as an example of well-written, well-designed Ruby code. Again, his other stuff is also interesting, like his blogging engine in 184 lines.

Speaking of Greg Brown and well-written code: Greg is the author of the book Ruby Best Practices, which uses real-life code examples from real-life Ruby projects to show what good, well-written, well-designed Ruby code looks like. (Plus, it also has a chapter called Ruby Worst Practices with anti-examples.) And, you don't even have to buy the book (although you really should), because all the code samples are on GitHub!

Jörg W Mittag
This is very helpful, Jörg!
A: 

Why not, like, write the 300 SLOC by yourself and then study them? :)

Leonid Shevtsov
I can write single methods. I have hard time writing methods that talk to each other--haven't wrapped my mind around the concept yet.