[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Could someone please explain what that "super" is?

Rubyist

2/2/2006 9:01:00 AM

Hi,

In order not to make an unnecessary crowd here, first I tried to learn
it myself, but I failed.
What is that "super" method? Could someone explain it please?

Thanks in advance.

3 Answers

Ross Bamford

2/2/2006 9:40:00 AM

0

On Thu, 2006-02-02 at 18:03 +0900, Rubyist wrote:
> What is that "super" method? Could someone explain it please?

# The 'super' commentary starts below. This is just a base-class.
class SomeClass
def initialize(one, two, three)
puts "Some: #{[one, two, three].inspect}"
end

def ameth(one)
"Some:ameth: #{one}"
end
end

# 1. When you override a method, you don't ever have to consider the
# inherited method if you don't want to.
class OtherClass < SomeClass
def initialize(*args)
puts "Other: #{args.inspect}"
end
end

# 2. Sometimes, though, you'll want to invoke the inherited method,
# e.g. to make sure any initialization gets done. For this you
# use the 'super' keyword, which says 'invoke the inherited method
# of this name'. Without args, super passes this method's arguments
# to the superclass method.
class FurtherClass < SomeClass
def initialize(*args)
super
puts "Further: #{args.inspect}"
end
end

# 3. If you're changing interfaces (with initialize) you might want to
# pass up different arguments, which you can do by passing them
# to super.
class LastClass < SomeClass
def initialize(a1,a2)
puts "Last: #{[a1,a2].inspect}"
super(a1,a2,3)
end

# 3.5. You can of course get the result from super and massage it
# as you need to when overriding methods.
def ameth(one)
s = super('ten')
"Last:ameth:#{s}"
end
end

# 4. You don't _have_ to use super. This is mostly equivalent from
# the user point of view (don't know about internally).
class Alternative < SomeClass
alias :__old_init :initialize
def initialize(one,two)
__old_init(one,two, 3)
puts "Alternative: #{[one, two].inspect}"
end
end

SomeClass.new(1,2,3)
>> # => Some: [1, 2, 3]

OtherClass.new(1,2,3)
>> # => Other: [1, 2, 3]

FurtherClass.new(1,2,3)
>> # => Some: [1, 2, 3]
>> # => Further: [1, 2, 3]

l = LastClass.new(:one, :two)
>> # => Last: [:one, :two]
>> # => Some: [:one, :two, 3]

puts l.ameth(55)
>> # => Last:ameth:Some:ameth: ten

Alternative.new(10,20)
>> # => Some: [10, 20, 3]
>> # => Alternative: [10, 20]

There's probably more stuff I forgot, but thats the basic gist of it.

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk



Santa Claus

12/28/2010 10:47:00 PM

0


"monkeywintest" <monkeywintest@yahoo.com> wrote in message
news:e1701489-8577-4ac9-ab06-b94147d8758a@l8g2000yqh.googlegroups.com...
>
>
> Phil wrote:
>> And the Obama/Democrat party must shoulder the blame. Obama's
>> relentless promotion of cap--and-tax--clean energy policy has stalled
>> inventment in many domestic projects. Obama is against off-shore
>> drilling off America's coasts.
>
> Hey dumb sack of shit, he gave the full go ahead for more drilling.

Monkyboy here is what happens when you get all your disinformation by
omission from Dan Rather, folks.

"Dumb sack of shit"

Gotta love it!

Bah hahahaha!!

"Obama Offshore Oil Moratorium Breaks Promise, Hurts Economy, Kills Jobs"

"Vast Swaths of Domestic Oil Production Shut Off"

"By Christine Hall "

"December 01, 2010 "

"Washington, D.C., December 1, 2010 - Today, the Obama Administration pulled
an about-face on its earlier plan to allow more domestic offshore oil
drilling. The Interior Department placed a moratorium on offshore drilling
off the Pacific, Atlantic, and eastern Gulf Coasts for at least seven years
and cited last year's BP oil spill as the excuse."

Now, better do an about face right quick and tell us why this is a good
thing because Hussein it!

Bah hahahahaha!!!!

http://cei.org/news-releases/obama-offshore-oil-moratorium-breaks-promise-hurts-economy-...

-Eddie Haskell


Santa Claus

12/28/2010 10:49:00 PM

0


"Phil" <claynorth@gmail.com> wrote in message
news:Xns9E5C5238AD41A4466gfdsfs@94.75.214.39...
> monkeywintest wrote
>
>>
>>
>> Phil wrote:
>
>> fuck you sacks of shit, if you will not work with Democrats except
>> when you want tax relief for the wealthy then you cant blame
>> Democrats and especially Obama for shit, now drop fucking dead.
>>
>
> But the right wing fever swamp propaganda media

Oh, look. A moron convention!

Bah hahahaha!!!

"Obama Offshore Oil Moratorium Breaks Promise, Hurts Economy, Kills Jobs"

"Vast Swaths of Domestic Oil Production Shut Off"

"By Christine Hall "

"December 01, 2010 "

"Washington, D.C., December 1, 2010 - Today, the Obama Administration pulled
an about-face on its earlier plan to allow more domestic offshore oil
drilling. The Interior Department placed a moratorium on offshore drilling
off the Pacific, Atlantic, and eastern Gulf Coasts for at least seven years
and cited last year's BP oil spill as the excuse."

Now, better do an about face right quick and tell us why this is a good
thing because Hussein it!

Bah hahahahaha!!!!

http://cei.org/news-releases/obama-offshore-oil-moratorium-breaks-promise-hurts-economy-...

-Eddie Haskell