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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:40:12+00:00 2026-05-31T02:40:12+00:00

I tried to to extend the code from this question for keeping records of

  • 0

I tried to to extend the code from this question for keeping records of an attribute value. However, my code fails in the case of more than one attributes. Here is the code:

class Class
  def attr_accessor_with_history(attr_name)
    attr_name = attr_name.to_s
    attr_reader attr_name

    ah=attr_name+"_history"
    attr_reader ah 

    class_eval %Q{          
      def #{attr_name}= (attr_name)
        @attr_name=attr_name

        if @ah == nil
          @ah=[nil]
        end
        @ah.push(attr_name)
      end
      def #{ah}
        @ah
      end  

      def #{attr_name}
        @attr_name
      end
     }
  end
end

Here a dummy class for testing

class Foo
  attr_accessor_with_history :bar
  attr_accessor_with_history :bar1
end

f = Foo.new
f.bar = 1
f.bar = 2
f.bar1 = 5
p f.bar_history  
p f.bar1_history  

For some reason, f.bar and f.bar1 both return 5 and f.bar_history = f.bar1_history = [nil, 1, 2, 5]. Any idea why that is?

  • 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-31T02:40:13+00:00Added an answer on May 31, 2026 at 2:40 am

    You were using @ah and @attr_name instead of @#{ah} and @#{attr_name} when getting/setting in the methods. This meant that they were always setting and returning the same instance variable, instead of different, dynamically named ones.

    class Class
      def attr_accessor_with_history(attr_name)
        class_eval %{
          attr_reader :#{attr_name}, :#{attr_name}_history
    
          def #{attr_name}=(value)
            @#{attr_name} = value
            @#{attr_name}_history ||= [nil]
            @#{attr_name}_history << value
          end
         }
      end
    end
    

    I’ve also generally cleaned up your code a little to make it (I think) clearer and more concise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this question is an extension of the question here i tried to extend Dane's
I've tried to extend the RequiredAttribute to make some localizations. I wrote this: public
Please bear with me for the length of this question, I tried to be
I already tried references from similar question on SO, but hasn't got the appropriate
This question is continuation from my earlier post titled selecting digits from regular expression.
After looking at this question , I tried the chosen answer myself! So basically
I tried to extend Socket object something as... public class TestSocket extends Socket {
I tried to use object expression to extend the IDelegateEvent, but in fsi there
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
Is it possible to extend dynamically javascript function scope? I tried without success the

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.