Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:16:07+00:00 2026-06-09T19:16:07+00:00

Imagine, you frequently need to wrap a set of methods from a super class

  • 0

Imagine, you frequently need to wrap a set of methods from a super class inside some static code or apects but with few lines results/different for each method, for example, wrap update notification around array methods to get an observable array.

My first thought, I want a class level method to be used like:

class MyArray < Array
  extend ObserverHelper # defines :wrap_method

  wrap_notify(:[]=) do |index_or_range, *args|
    [self[index_or_range], super(index_or_range, *args)]
    # much nicer it would be to define
    # removed = self[index_or_range]
    # added = super(index_or_range, *args)
    # but this makes it even more complicated (suggestions welcome!)
  end
...
end

So far so good. How to implement :wrap_method now? My most promising approach reached this far. Does not work because of super:

require 'observer'

module ObserverHelper
  include Observable

  def wrap_notify(meth, &block)
    define_method meth,->(*args) {
      removed, added = instance_exec(*args, &block)
      changed
      notify_observers(removed, added)
      added
    }
  end
end

Errors:

  • If defined directly on array subclass:
    super called outside of method

  • If defined in an module and included to array:
    “super from singleton method that is defined to multiple classes is not supported; this will be fixed in 1.9.3 or later” or “Segmentation fault” if the method has the proper return value)

I searched some time but didn’t find an advisable solution to change the binding of a closure, however, maybe I miss something or you can solve it without?

Rails does those things by evaluating strings, not sure yet If I would like to got this way. Performance is not yet important but will be. Thus, I need feedback about the different possibilities and performance issues.

Finally, I got two more verbose solutions running. First, pass the instance, no super and no self:

  wrap_observer(:[]=) do |instance, index_or_range, *args|
    [instance[index_or_range], instance.send(meth, index_or_range, *args)]
  end

The other one uses wrap_observer as instance_method:

  def []=(index_or_range, *args)
    wrap_observer do
      [self[index_or_range], super(index_or_range, *args)]
    end
  end

which I regard as preferable.

A solution for the DSL may be to define the methods without the wrapper and than iterate through the defined methods adding it.

Are there more ways to solve this performant and maintainable with my intended DSL?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:16:09+00:00Added an answer on June 9, 2026 at 7:16 pm

    The only way to rebind a block is to define a method with it, which also allows to use super. So, you would want to define two methods — something that can be done using modules:

    module ObserverHelper
      def wrap_notify(meth, options={}, &block)
        override = Module.new do
            define_method(meth, &block)
        end
        notifier = Module.new do
            define_method meth do |*args|
                removed, added = super(*args)
                options ||= {}
                changed
                notify_observers(removed, added)
                after
            end
        end
        include override
        include notifier
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine you have class A which has code which runs as method M. And
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine I've got a database with lots of data, from which users can search.
Imagine a very big gwt project of application. This is just a some form
Imagine I have a table with two columns, a primary key and some data.
I've got some bad bots targeting my website and I need to dynamically handle
This might be a pretty straightforward question, but I'm trying to understand some of
Whole day I was looking for some tutorial or piece of code, just to
I want a partial view that display some stuff from a website that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.