From: Robert Dober Date: 2008-08-01T02:37:51+09:00 Subject: Re: super with block On Thu, Jul 31, 2008 at 7:10 PM, Lou Zell wrote: >> Hi, >> >> This worked for me: >> >> irb(main):023:0> class NString < String >> irb(main):024:1> def gsub!(*args, &blk) >> irb(main):025:2> super(*args, &blk) >> irb(main):026:2> end >> irb(main):027:1> end >> => nil >> irb(main):028:0> s = NString.new("hi") >> => "hi" >> irb(main):029:0> s.gsub!(/(hi)/){$1} >> => "hi" >> >> Hope this helps, >> >> Jesus. > > > Hi Jesus, thank you for your response. Unfortunately, I could not > replicate your results: > > >>> class NString < String >>> def gsub!(*args, &blk) >>> super(*args, &blk) >>> end >>> end > => nil your problem has nothing to do with passing the block along in gsub! >>> s = NString.new("hi") guess what s is here? If you guessed "", you guessed correctly > => "hi" >>> s.gsub!(/(hi)/){$1} > => "" normal given that s was "" Probably subclassing is not what you want, but it can be accomplished of course > > > Perhaps it is my version of ruby? > ~$ ruby -v > ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] > > > Lou > -- > Posted via http://www.ruby-forum.com/. > > Here is the final solution class NString < String def initialize str replace str end def gsub! *args super( *args, &Proc::new ) # that is just to show you yet another way to do this but passing &blk is better end end n = Nstring::new( "123") p n p n.gsub!(/.../){ |m| m.reverse } # $1 is not set as somebody did point out already HTH Robert -- http://ruby-smalltalk.blogspot.com/ There's no one thing that's true. It's all true. -- Ernest Hemingway