strange: 'super' is not the parent class

  • Thread starter Gioele Barabucci
  • Start date
G

Gioele Barabucci

When I try to run the attached code (ar2.rb) with ruby 1.8.2 I get the
following error:

../ar2.rb:41:in `packDecl': undefined method `packDecl' for
"\000\000\000\005\000\000\000\002":String (NoMethodError)
from ./ar2.rb:27:in `pack'
from ./ar2.rb:54
from ./ar2.rb:49:in `each'
from ./ar2.rb:49

It seems like "\0\0\0\5\0\0\0\2".packDecl is called instead of
super.packDecl. Did I overwrite 'super'? Where?



#!/usr/bin/env ruby
require 'pp'

BASE_INFO = 0x2

class StreamData
attr_accessor :typeDataLength
attr_accessor :type

attr_accessor :payloadLength

def initialize
@typeDataLength = 0
@payloadLength = 0
end

def packDecl
length = @typeDataLength + @payloadLength
return [length, @type].pack("LI")
end

def packTypeData
return Array.new
end

def pack
return packDecl + packTypeData
end
end

class BaseInfoData < StreamData
attr_accessor :name

def initialize
super
@type = BASE_INFO
end

def packDecl
@typeDataLength = [@name].pack("a*").length
return super.packDecl
end

def packTypeData
return [@name].pack("a*")
end
end

["happy", "two"].each { |file|

baseInfo = BaseInfoData.new()
baseInfo.name = file

pp baseInfo.pack
}
 
D

David A. Black

Hi --

When I try to run the attached code (ar2.rb) with ruby 1.8.2 I get the
following error:

../ar2.rb:41:in `packDecl': undefined method `packDecl' for
"\000\000\000\005\000\000\000\002":String (NoMethodError)
from ./ar2.rb:27:in `pack'
from ./ar2.rb:54
from ./ar2.rb:49:in `each'
from ./ar2.rb:49

It seems like "\0\0\0\5\0\0\0\2".packDecl is called instead of
super.packDecl. Did I overwrite 'super'? Where?

def packDecl
length = @typeDataLength + @payloadLength
return [length, @type].pack("LI")
end
[...]


def packDecl
@typeDataLength = [@name].pack("a*").length
return super.packDecl
end

super here calls the previous packDecl, which returns a string. You
then call the method packDecl on that string -- which is where you get
the undefined method error.

I think you just want "super" here, not "super.packDecl".


David
 
C

Charles Steinman

Gioele said:
When I try to run the attached code (ar2.rb) with ruby 1.8.2 I get the
following error:

./ar2.rb:41:in `packDecl': undefined method `packDecl' for
"\000\000\000\005\000\000\000\002":String (NoMethodError)
from ./ar2.rb:27:in `pack'
from ./ar2.rb:54
from ./ar2.rb:49:in `each'
from ./ar2.rb:49

It seems like "\0\0\0\5\0\0\0\2".packDecl is called instead of
super.packDecl. Did I overwrite 'super'? Where?



#!/usr/bin/env ruby
require 'pp'

BASE_INFO = 0x2

class StreamData
attr_accessor :typeDataLength
attr_accessor :type

attr_accessor :payloadLength

def initialize
@typeDataLength = 0
@payloadLength = 0
end

def packDecl
length = @typeDataLength + @payloadLength
return [length, @type].pack("LI")
end

def packTypeData
return Array.new
end

def pack
return packDecl + packTypeData
end
end

class BaseInfoData < StreamData
attr_accessor :name

def initialize
super
@type = BASE_INFO
end

def packDecl
@typeDataLength = [@name].pack("a*").length
return super.packDecl

The problem is that super is not an object; it's the superclass'
implementation of the current method. So you are calling the method
packDecl on the result of StreamData's packDecl method, which is a
packed data string.
 
T

Trans

Yes, very strange... Do you ever wonder about these names? I do. Why is
super called "super" anyway? Is it really "all that"? The term
'superclass' is surely nonesense. How can it be a superclass when
clearly it's the functional subset of the subclass. It's the subclass
that's the superset of the superclass. Obviously. What super programmer
got that all turned around, heh? I bet he was subcontracted. I just
bet. Hmmm... Perhaps it all has to do with Parents actually. You know,
we call superclasses parents too. But we all know, we're better than
our parents; natural Darwinian order and all that. Besides I just look
better than my parents ;) Youth is super. Old age sucks. Maybe therein
lies the catch. We don't want to admit "that". After all, we're all
getting old (and fast). So we put these little messages here and about.
You know, like in that Bible commandment or on Father's Day cards, "To
a Super Dad!". On badges, keyrings and fridge magnets, "Super Mum!"[1]
And, yes, we even put them in our programs --especially those Ruby
programs. Super here. Super there. I suppose it just makes us feel
better about the whole affair --growing old, instantiating new
children, being cleaned-up by the great Garbage Collector in the sky.
Sigh... Well, maybe our classes feel better. Maybe I'll send one a card
just to be sure.

irb(main):019:0> def card
irb(main):020:1> "You're a #{superclass}!"
irb(main):021:1> end
=> nil
irb(main):022:0> Class.send :card
=> "Your a Module!"

What!?

T.

Okay, maybe I should call it a night. "Night!" :)


[1] http://www.badgeplanet.co.uk/detail.asp?badgeID=446
 
G

Gioele Barabucci

Il Thu, 4 Aug 2005 09:11:05 +0900, David A. Black scriveva:
def packDecl
@typeDataLength = [@name].pack("a*").length
return super.packDecl
end

I think you just want "super" here, not "super.packDecl".
You are correct, 'super' is all I need.

I was thinking that 'super' was an object like 'self', but seen with the type
of its superclass instead of its own.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top