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

  • Home
  • SEARCH
  • 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 9138745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:17:49+00:00 2026-06-17T09:17:49+00:00

I would like to override Item`s find_items method in UserItem subclass. Should I add

  • 0

I would like to override Item`s find_items method in UserItem subclass.
Should I add that method as protected or private?

I now that protected methods can be used in subclasses and private only in class to which they belong.

class Item
  def item_ids
    @ids ||= REDIS.zrevrange(key, 0, 100).map(&:to_i)
  end

  def items
    find_items
    item_ids.collect {|id| records.detect {|x| x.id == id}}.compact.map(&:content)
  end

protected
  def items_scope
    Item
  end

  def find_items
    items_scope.find(item_ids)
  end
end

class UserItem < Item
  def initialize(user)
    @user = user
  end
  # should I add it here?
protected
  # or here?
  def items_scope
    Item.where(user_id: @user.id).not_anonymous
  end
end

Method overwriting:

def find_items
  items_scope.where(id: item_ids)
end
  • 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-17T09:17:50+00:00Added an answer on June 17, 2026 at 9:17 am

    Semantics in Ruby differ from what you may have used to. Actually, private
    means that you cannot specify receiver explicitly for the method, but you can
    use it from derived classes as well, as long as you don’t specify receiver. Thus

    class A
      def bar; 42; end
      private :bar
    end
    
    class B < A
      def foo; bar; end
    end
    

    and B.new.foo works just fine. So, private in Ruby is rather close to protected
    in other OO languages. I don’t even remember what protected was in Ruby; it is very
    seldom used in Ruby.

    In your example I wouldn’t use private for find_items. I would leave it public or turn it into mixin (because it doesn’t use instance variables)

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

Sidebar

Related Questions

I have a class MyClass, and I would like to override the method ToString()
I would like to override default Admin Panel result list (change_list_results.html) to add ID
I would like to override the default fetch() method in a Backbone model, thus
I would like to override one method onMouseUp:function(evt){ if(evt.target===this.domNode||!this._highlighted_option){ return; }else{ if(evt.target==this.previousButton){ this.onPage(-1); }else{
I am working with an API that exposes an override like this SuperAwesomeNinjaMethod(ItemToProcess item,
I would like to get the position in the list from the item that
I would like to temporarily override the kill-new function. I have a way I
Would like a for loop in jquery so that: For every hover_link: show hidden
Would like to know what a programmer should know to become a good at
Would like to make anapplication in Java that will not automatically parse parameters used

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.