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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:51:20+00:00 2026-05-15T19:51:20+00:00

Essentially I’m wondering how to place callbacks on objects in ruby, so that when

  • 0

Essentially I’m wondering how to place callbacks on objects in ruby, so that when an object is changed in anyway I can automatically trigger other changes:

(EDIT: I confused myself in my own example! Not a good sign… As @proxy is a URI object it has it’s own methods, changing the URI object by using it’s own methods doesn’t call my own proxy= method and update the @http object)

class MyClass
  attr_reader :proxy
  def proxy=(string_proxy = "")
    begin
      @proxy = URI.parse("http://"+((string_proxy.empty?) ? ENV['HTTP_PROXY'] : string_proxy))
      @http = Net::HTTP::Proxy.new(@proxy.host,@proxy.port)
    rescue
      @http = Net::HTTP
    end
  end
end

m = MyClass.new
m.proxy = "myproxy.com:8080"
p m.proxy
# => <URI: @host="myproxy.com" @port=8080>

m.proxy.host = 'otherproxy.com'
p m.proxy
# => <URI: @host="otherproxy.com" @port=8080>
# But accessing a website with @http.get('http://google.com') will still travel through myproxy.com as the @http object hasn't been changed when m.proxy.host was.
  • 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-15T19:51:20+00:00Added an answer on May 15, 2026 at 7:51 pm

    I managed to figure this one out for myself!

    # Unobtrusive modifications to the Class class.
    class Class
      # Pass a block to attr_reader and the block will be evaluated in the context of the class instance before
      # the instance variable is returned.
      def attr_reader(*params,&block)
        if block_given?
          params.each do |sym|
            # Create the reader method
            define_method(sym) do
              # Force the block to execute before we…
             self.instance_eval(&block)
              # … return the instance variable
              self.instance_variable_get("@#{sym}")
            end
          end
        else # Keep the original function of attr_reader
          params.each do |sym|
            attr sym
          end
        end
      end
    end
    

    If you add that code somewhere it’ll extend the attr_reader method so that if you now do the following:

    attr_reader :special_attr { p "This happens before I give you @special_attr" }
    

    It’ll trigger the block before it gives you the @special_attr. It’s executed in the instance scope so you can use it, for example, in classes where attributes are downloaded from the internet. If you define a method like get_details which does all retrieval and sets @details_retrieved to true then you can define the attr like this:

    attr_reader :name, :address, :blah { get_details if @details_retrieved.nil? }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Ajax Control Toolkit has an auto complete extender (http://www.asp.net/ajaxlibrary/act_AutoComplete.ashx).… May 15, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer If you're using SimpleCursorAdapter or something similar, each row of… May 15, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer ok, now i know where is the error, is not… May 15, 2026 at 7:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.