Class variables are Mysterious
June 14th, 2005I was just reading about Class variables, which, for some reason, don’t make an awful lot of intuitive sense to me… so, how important are they? Being a statistically-minded guy, when in doubt, I count.
UPDATED below, thanks sbp! (The script immediately following gives the right results, but only by chance. Find the bug? 8^) )
$ pwd /usr/lib/ruby/1.8 $ grep -c '@@' *.rb |rev|sort -n |rev|tac|head
| weakref.rb | 17 |
| yaml.rb | 16 |
| profiler.rb | 12 |
| gserver.rb | 11 |
| tempfile.rb | 5 |
| tmpdir.rb | 4 |
| set.rb | 3 |
| resolv.rb | 2 |
| matrix.rb | 0 |
| complex.rb | 0 |
$ ls -alh *.rb |wc -l 80
Well, only 8 out of 80 files in the Ruby directory have Class variables, so I’m not going to go insane about it. I mean, I’ll learn it but… that gives me a little perspective .
UPDATE
Sean B. Palmer pointed via a comment and instant messaging that there’s a problem with my little script: Uh, it doesn’t sort right. It’s sorting reversed numerals. Funnily enough, the data I had (the number of hits for '@@' in those Ruby files) sorted the same way reversed.
sbp’s solution doesn’t suffer from this thinko:
$ grep -c '@@' *.rb | awk -F: '{print "$2" "$1"}' | sort -rn | head
Which gives the same results as mine borked commandline. But it’s right.
Ah, blogging! The debugger of the future!