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.
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.
On Thu, 2006-02-02 at 18:03 +0900, Rubyist wrote:
What is that “super” method? Could someone explain it please?
class SomeClass
def initialize(one, two, three)
puts “Some: #{[one, two, three].inspect}”
end
def ameth(one)
“Some:ameth: #{one}”
end
end
class OtherClass < SomeClass
def initialize(*args)
puts “Other: #{args.inspect}”
end
end
class FurtherClass < SomeClass
def initialize(*args)
super
puts “Further: #{args.inspect}”
end
end
class LastClass < SomeClass
def initialize(a1,a2)
puts “Last: #{[a1,a2].inspect}”
super(a1,a2,3)
end
def ameth(one)
s = super(‘ten’)
“Last:ameth:#{s}”
end
end
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs