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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:04:24+00:00 2026-05-17T19:04:24+00:00

I have some Ruby code that uses protected level attr_readers to be able to

  • 0

I have some Ruby code that uses protected level attr_readers to be able to implement an == method so that we can assert some resultant Calendar is equal to an expected Calendar), but those protected accessors would not be needed for any code besides assertions in test code.

Some code I have looks like this:

class Calendar    
  def initialize(date_times)
    @date_times = date_times
  end

  def next_date_time
    @date_times.find { |time| time - DateTime.now > 0 }
  end

  def ==(other)
    @date_times == other.date_times
  end

  protected

  attr_reader :date_times
end

How can I avoid the need for protected attr_readers? Actually, ideally it would be great if I could remove the need for the def==(...... as well, since it is also only needed for tests!

All the Best,
Alex

A First Stab At This:

module Kernel
  def subclass_with_equals(class_symbol)
    clazz = Kernel.const_get(class_symbol.to_s)
    Kernel.subclass_with_default_equals(clazz)
  end

  private

  def self.subclass_with_default_equals(base_class)
    sub_class = Class.new(base_class)
    sub_class.class_eval do
      def ==(other)
        instance_variables.all? { |v| self.instance_variable_get(v) == other.instance_variable_get(v) } and other.instance_of? self.class
      end
    end
    sub_class
  end
end

And at the top of my spec file I can add this line to add a default == method to my class:

Calendar = sub_class_with_equals :Calendar

This enables me to remove five or six lines from my Calendar class!

  • 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-17T19:04:25+00:00Added an answer on May 17, 2026 at 7:04 pm

    You could monkey-patch the class from within the test code, and at that point you may as well get rid of the #protected() call.

    Even better might be to, from within the test code, create a subclass of Calendar that has an accessor and == method.

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

Sidebar

Related Questions

I have just optimised some Ruby code that was in a controller method, replacing
I have to implement some code in Ruby based on Java implementation that was
I have some Ruby code which takes dates on the command line in the
I have some javascript code that is used in a few different pages in
I have written a module that has some generic, reusable code that I would
Let's say I have the following ruby code : def use_object(object) puts object.some_method end
I am running Ruby and MySQL on a Windows box. I have some Ruby
I have been using ActiveResource in my Ruby on Rails applications for some time,
I've done some Python but have just now starting to use Ruby I could
I have to parse a tab-delimited text file with Ruby to extract some data

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.