Learning from the standard library

There are a lot of good ways to learn how to do things in Ruby. I
happen to think the Pickaxe (http://www.pragmaticprogrammer.com/ruby/)
is a great place to start. But at a certain point, it isn’t enough.
For me, when it comes to open-source languages, the next step is often
looking at examples, in particular, the standard library.

When I first started doing this with Ruby, one thing hit me right away.
Most of the standard library files use the shortcut:

class << self

end

This is one of the many ways of defining class methods in Ruby, but
while it is extremely common in the standard library, it’s one that I
don’t believe is mentioned in the Pickaxe. In addition to this, there
were all kinds of other interesting, but odd things lurking in the
standard library, like instance_eval.

So, what I’m wondering is this. Do other people use the standard
library as a place to learn how to do things? If not, why not? If so,
which files do you find most useful, most confusing, most well written, etc?

What I’ve noticed:

singleton.rb: I am amazed by how small it is, but the magic is deep and
complex. Using an instance eval, but using the %{} syntax is really
tricky. There are no comments to help a newcomer learn, or a maintainer
maintain, but once you get it, it is pretty obvious what’s happening.

date.rb: At first glance, it appears pretty simple, but then there’s the
"once" method. It is a triky one too. Once again, no comments to speak of.

profiler.rb: Ouch. Scary. I wouldn’t expect a profiler to be easy to
understand, but this one is really opaque.

net/http.rb: Really well documented. Has some great examples of using
classes well, and shows a lot of the language flexibility, but the
comments are somewhat sparse.

Btw, I’m basing this off Ruby 1.6.8. I haven’t gotten around to
installing 1.8 on this machine yet because I haven’t found RedHat 9 RPMs.

Ben

Hi –

There are a lot of good ways to learn how to do things in Ruby. I
happen to think the Pickaxe (http://www.pragmaticprogrammer.com/ruby/)
is a great place to start. But at a certain point, it isn’t enough.
For me, when it comes to open-source languages, the next step is often
looking at examples, in particular, the standard library.

Definitely – but don’t forget, just to give full credit, the Pickaxe
itself leads the way here, by taking some of its examples from the
standard distribution.

When I first started doing this with Ruby, one thing hit me right away.
Most of the standard library files use the shortcut:

class << self

end

This is one of the many ways of defining class methods in Ruby, but
while it is extremely common in the standard library, it’s one that I
don’t believe is mentioned in the Pickaxe. In addition to this, there
were all kinds of other interesting, but odd things lurking in the
standard library, like instance_eval.

So, what I’m wondering is this. Do other people use the standard
library as a place to learn how to do things? If not, why not? If so,
which files do you find most useful, most confusing, most well written, etc?

Actually one thing I find myself doing sometimes is grepping the
library when I’m curious about how common a given idiom is (or isn’t).
Obviously there’s a lot of other Ruby code in the world, but I’m
always curious to see what’s being done in the distribution.

For example:

How common is each_with_index?

$ grep ‘each_with_index’ find . -name "*.rb" | wc -l
10

rough stats on camelCase method names

$ grep ‘def [a-z]+’ find . -name "*.rb" |
wc -l
4630
$ grep ‘def [a-z]+[A-Z]+’ find . -name "*.rb" | wc -l
44

and so on. It can actually be quite informative and interesting.

David

···

On Fri, 22 Aug 2003, Ben Giddings wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

[…]

class << self

end

This is one of the many ways of defining class methods in Ruby, but
while it is extremely common in the standard library, it’s one that I
don’t believe is mentioned in the Pickaxe. In addition to this, there
were all kinds of other interesting, but odd things lurking in the
standard library, like instance_eval.

It’s a standard idiom - not one of my favourites, but there you go -
and I’m sure any update to the pickaxe will mention it. Look at
ClassMethods on the Wiki for a roundup.

[…]

Btw, I’m basing this off Ruby 1.6.8. I haven’t gotten around to
installing 1.8 on this machine yet because I haven’t found RedHat 9 RPMs.

It’s worthwhile getting Ruby via anonymous CVS if you’re interested in
getting the latest source documentation. It’s not perfect, but it’s
improving, and it’s better than 1.6.8!

Note that only API is documented, though, not curly implementation
methods, which I agree are abundant.

Gavin

···

On Friday, August 22, 2003, 2:42:09 AM, Ben wrote:

I’d agree that this is a standard learning curve: You start with a
tutorial or with a book. You practice. You want to dig into details
and get an idea about what’s common practice.

Ruby could learn from Smalltalk in this place. The library is always
present because of the browser’s view of the complete class
repository. Any pattern which can’t be found in the library is said to
be without example.

Cheers
Sascha

···

Ben Giddings ben@thingmagic.com wrote:

There are a lot of good ways to learn how to do things in Ruby. I
happen to think the Pickaxe (http://www.pragmaticprogrammer.com/ruby/)
is a great place to start. But at a certain point, it isn’t enough.
For me, when it comes to open-source languages, the next step is often
looking at examples, in particular, the standard library.

Perhaps on page 252…

···

On Friday, August 22, 2003, at 02:31 AM, Gavin Sinclair wrote:

class << self

end

It’s a standard idiom - not one of my favourites, but there you go -
and I’m sure any update to the pickaxe will mention it.

::furiously checks…:: Touche!

···

On Friday, August 22, 2003, 10:40:37 PM, Dave wrote:

On Friday, August 22, 2003, at 02:31 AM, Gavin Sinclair wrote:

class << self

end

It’s a standard idiom - not one of my favourites, but there you go -
and I’m sure any update to the pickaxe will mention it.

Perhaps on page 252…

Saluton!

  • Dave Thomas; 2003-08-22, 19:04 UTC:

class << self

end

It’s a standard idiom - not one of my favourites, but there you go

  • and I’m sure any update to the pickaxe will mention it.

Perhaps on page 252…

Please provide titles and section numbers but not pages because
there are people who

  • use an HTML version of the pickaxe

  • use a translation of the pickaxe

The former does not exclude the latter and vice versa. In the German
printed edition it is page 296 that points out that you are not
forced to use the ‘<<’ idiom.

It’s in chapter ‘19 Klassen und Objekte’ (‘Classes and Objects’),
subsection ‘19.1 Wie Klassen und Objekte interagieren’ (‘How Classes
and Objects Interact’), subsubsection ‘19.1.3 Objektspezifische
Klassen’ (‘Object Specific Classes’).

Gis,

Josef ‘Jupp’ Schugt

···

On Friday, August 22, 2003, at 02:31 AM, Gavin Sinclair wrote:

LICENSE AGREEMENT: By adding my mail address to your Outlook or
Outlook Express address book you accept paying me 10 EUR for each
message containing malware that I receive from you.