Statistics module problem


#1

Here’s a code fragment from the statistics module from Ruby Forge.

code begins here

module Math
module Statistics
VERSION = “2001_02_18”
def self.append_features(mod)
unless mod < Enumerable
raise TypeError,
“`#{self}’ can’t be included non Enumerable (#{mod})”
end
def mod.default_block= (blk)
self.const_set(“STAT_BLOCK”, blk)
end
def mod.default_block
defined?(self::STAT_BLOCK) && self::STAT_BLOCK
end
super
end
def default_block
@stat_block || type.default_block ## <—???
end
def default_block=(blk)
@stat_block = blk
End

code ends

When I run the test program, I get the following warning message:

warning: Object#type is deprecated; use Object#class

So I replaced the line that reads “@stat_block || type.default_block”
with
@stat_block || class.default_block” and ran the same script and now I
get
the following error message:

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
`require__’:
c:/ruby/lib/ruby/site_ruby/1.8/math/statistics.rb:162: syntax error
(SyntaxError)

    @stat_block || class.default_block
                         ^  from

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:18:in
`require’ from statistics-demo.rb:1

Please help me understand what “type.default_block” does?

Thank you,

–Craig


#2

On Apr 6, 2006, at 10:08 AM, Craig K. wrote:

So I replaced the line that reads “@stat_block ||
type.default_block” with
@stat_block || class.default_block” and ran the same script and
now I get
the following error message:

The problem is unique to the method ‘class’ because the parser
confuses the method
call with the use of ‘class’ as a keyword to start a class/end
block. The solution
is to disambiguate the case with an explicit call to self:

type.default_block 		#deprecated
class.default_block		#syntax error, parser gets confused
self.class.default_block	# all is well

Personally I wish the parser was smarter (but maybe it is a grammar
problem?) or that
the method wasn’t called ‘class’ to avoid the ambiguity. For some
reason I just don’t
like the extraneous ‘self’.

Gary W.