Class << self

rubyists-

can someone please confirm that this

class Foo
class << self
def bar
self
end
end
end

class Bar < Foo
end

puts Foo.bar # >> Foo
puts Bar.bar # >> Bar

is the correct way to code an inheritable class method?

why would

def Foo.foo
‘foo’
end

NOT be inherited in this fashion? i understand that the methods are statically
tied to the Foo class object in this case… but it seems like the interpreter
should work around this based on pols?

-a

···

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

ahoward wrote:

rubyists-

can someone please confirm that this

class Foo
class << self
def bar
self
end
end
end

class Bar < Foo
end

puts Foo.bar # >> Foo
puts Bar.bar # >> Bar

is the correct way to code an inheritable class method?

why would

def Foo.foo
‘foo’
end

NOT be inherited in this fashion? i understand that the methods are statically
tied to the Foo class object in this case… but it seems like the interpreter
should work around this based on pols?

Seems to work for me:

puts Bar.foo # ==> ‘foo’

Or did you mean something else?

ahoward ahoward@fsl.noaa.gov writes:

why would

def Foo.foo
‘foo’
end

NOT be inherited in this fashion? i understand that the methods are statically

Um… it works for me.

irb(main):001:0> class Foo; end
nil
irb(main):002:0> def Foo.abc
irb(main):003:1> ‘abc’
irb(main):004:1> end
nil
irb(main):005:0> class Bar < Foo
irb(main):006:1> end
nil
irb(main):007:0> Foo.abc
“abc”
irb(main):008:0> Bar.abc # => “abc” because method “abc” is ‘inherited’
“abc”