From: Robert Klemme Date: 2010-09-16T15:20:15+09:00 Subject: Re: access super from yield block? On 15.09.2010 22:12, Gene Angelo wrote: >> What exactly are trying to achieve? > > I have class A that derives from class B > > A#parse and A#parse! override members of the same name in B > > A#parse and A#parse! both call A#do_parse to retrieve arguments from the > command line in a loop. A yield in A#do_parse returns the arguments > (argv). I want to call 'super argv' from both A#parse and A#parse! but > super is not set to A#super. What should "super argv" do? Your description is completele technical but you don't tell what the problem is that you are trying to solve here. > # simplified... > class A< B > .. > > def parse > do_parse {|a| super a} > end > > def parse! > do_parse {|a| super a} > def Where's the difference between #parse and #parse!? Also, why do you want to call the superclass method from the block? This will result in parsing input twice, since B#parse will also call do_parse. This does not seem to make much sense And, btw. reading from stdin from within method #do_parse is not very modular. It might turn out that you want to read input from a file and then you need to break this up anyway. > def do_parse > argv = gets.strip.to_a This will create an Array with a single element. Is this really what you want? > return ['-x'] if argv.index '-x' > return ['-h'] if argv.empty? > yield argv > end > protected :do_parse > > .. > end And: what does the whole picture look like? Why do you want several classes that somehow must behave differently but apparently share the same argument parsing code? I would have expected different classes to have different needs for arguments to be parsed and consequently not share parsing code. With OptionParser you could do: class B def parse(argv) OptionParser.new do |opts| fill opts end.parse argv end def parse!(argv) OptionParser.new do |opts| fill opts end.parse! argv end protected def fill(opts) opts.on '-x' do |...| end opts.on '-y' do |...| end end end class A < B def fill(opts) super opts.on '-z' do |...| end end end Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/