Search code examples
rubyinheritanceparentsuper

Ruby - Can I choose a location to put some code in a method inherited with Super?


I will put here an example:

class A

    def go(name = "girls")

        print "hello "
        print name
        puts " !"
    end
end

class B < A

    def go

        super("boys")
    end
end

A.new.go
B.new.go

result:

hello girls !
hello boys !

I need to do that but not with just params changing but adding some variables:

class A

    def go(name = "girls")

        dad_variable = "coders"

        print "hello "

        superAnchor("myAnchor") # put a "super anchor" here

        puts " !"
    end
end

class B < A

    def go

        superAnchor("myAnchor"){
            print dad_variable # do some code in relation with Daddy
            # do some other things but with the parent variables
            # interacting with the inherited environnement
        }
    end
end

A.new.go
B.new.go

result:

hello girls !
hello coders !

In summary I want to know if we can put anchor in the son class who inherit of the parent method and put some codes in association with the position of the anchors in the parent method and the environnement variables of the parent method ?

Is there a system to do that ? Thank you guys :)


Solution

  • Boys, I think, I find, that's the yield system

    http://fr.wikipedia.org/wiki/Yield_(instruction) (sorry for the french source)

    but I am not really sure...