This post originated from an RSS feed registered with Ruby Buzz
by Eigen Class.
Original Post: More about naming conventions
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
I patched ruby 1.8.4 to gather some statistics regarding naming conventions, as far as identifiers are concerned. My
local build will now emit such information at parse time as follows:
$ ruby -t -c -e "def a(x); c = x.size; def x.bar; 1 end; c + 1 end"
[tokinfo] method def: a
[tokinfo] tIDENTIFIER assignment for lvar: c
[tokinfo] singleton method def: bar
Syntax OK
These are the statistics for Ruby 1.8.4's stdlib:
type
average length
std dev
count
symbol
7.0
4.3
3214
ivar
8.6
3.9
1915
smethod
9.6
4.9
1001
gvar
9.1
4.3
377
cname
8.3
4.8
2124
cvar
11.4
4.4
103
dvar_curr
3.5
2.7
1158
constant
10.9
5.0
1660
dvar
4.7
3.7
49
method
10.5
5.6
7566
lvar
4.3
2.9
3427
I just added some code to the parser and some support functions in order to capture:
uses of symbols (symbol)
method definitions (method)
singleton method definitions in the form def object.meth (smethod)
class/module names (cname)
plain constant names (constant)
block-local variables assigned to inside a nested block (dvar)
block-local variables (dvar_curr)
local variables (lvar)
instance variables (ivar)
global variables (gvar)
class variables (cvar)
The difference between dvar and dvar_curr is fairly subtle; in the following example,
x is a dvar_curr:
%w[a b c].each{|x|putsx*2}
"curr" means that the variable was defined in the current block.
When a block reuses a variable defined in an enclosing block, it is considered
a dvar: