Jesús Gabriel y Galán
7/31/2008 9:56:00 AM
On Thu, Jul 31, 2008 at 11:06 AM, Lou Zell <lzell11@gmail.com> wrote:
> Hi all,
>
> I'm looking for a bit of help. I can't seem to get the block to "pass
> through" to the parent class. I figured without an '&' in the parameter
> list there would be no conversion to proc but maybe that is not the
> case.
>
> class Nstring < String
> def gsub!(*args)
> super
> end
> end
>
> This works as I expected:
>
> s = Nstring.new("hi")
> s.gsub!(/(hi)/,'\1')
> => "hi"
>
> But this, not so much:
>
> s.gsub!(/(hi)/) {$1}
> => ""
>
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.