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 7032071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:53:12+00:00 2026-05-28T00:53:12+00:00

I am trying to add a method to Observable, so that for a class

  • 0

I am trying to add a method to Observable, so that for a class that includes it, it can call the method observe_attribute :attribute that would generate a method attribute= with the logic to check to see if the value has changed:

module Observable
  def observe_attribute(attribute)
    raise NameError, "#{self.class} does not contain #{attribute}" unless instance_variables.include? "@#{attribute}"
    eval %" def #{attribute}=(new_value) 
              unless @#{attribute} == new_value
                changed
                notify_observers
                puts 'ok'
              end
            end
         ";
  end
end

However, for the following class, the last call to observe_attribute :att results in a NoMethodError, while the commented one does not:

class Test 
  include Observable

  def initialize
    @att = 3
    #observe_attribute :att
  end

  observe_attribute :att
end

What would I need to do in order for the last call to observe_attribute to work correctly?

  • 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-05-28T00:53:13+00:00Added an answer on May 28, 2026 at 12:53 am

    When you include a module, the methods contained in it are loaded into the “instance” level of methods.

    If you want to include methods from a module into the Class scope, you have to extend the module.

    Or if you need both, but you want to package it as a single “include”, you can extend the “included” method of the module you’re extending and do a bit of magic with it by extending and including 2 modules from there.

    module Observable
    
      def self.included(other)
        other.send(:include, InstanceMethods)
        other.send(:extend, ClassMethods)
      end
    
      module InstanceMethods
        def some_helper
          puts "called some_helper"
        end
        def other_helper
          puts "called other_helper"
        end
      end
    
      module ClassMethods
        def observe_attribute(name)
          # do something useful, maybe in your case define the method
          define_method("#{name}=") do |val|
            puts "called #{name}= #{val}"
            # bla bla
            some_helper
            other_helper
            # end
            puts "done"
          end
        end
      end
    
    end
    
    class Foo
      include Observable
      observe_attribute :foo
    end
    

    Now you can call …

    o = Foo.new
    o.foo = :bar
    

    And then the result would be …

    called foo= bar
    called some_helper
    called other_helper
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to add method or variable declarations to org.eclipse.jdt.core.dom.CompilationUnit, but I can't
I am trying to add the following method to the Math class in Ruby
I am trying to add a method that validate a field to see if
I'm trying to add a payment method to working website that is using a
I'm making a observable class. The Add methods works fine. But then I'm trying
I'm trying to add a method to a jQuery object that has the same
I am trying to use (*env)->RegisterNatives to add methods to a defined class which
I'm trying add a tab to my web page that looks like this: Using
Trying to add an onclick handler to my tabs, and can't seem to get
trying to add an ExpiresDefault ExpiresByType to content on my website so that way

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.