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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:50:02+00:00 2026-05-31T23:50:02+00:00

I have the need to use a Stack-like data structure for a program that

  • 0

I have the need to use a Stack-like data structure for a program that I am writing and I know that Ruby doesn’t have an explicit Stack data-structure, but that the Array class has all of the properties that make a Stack: push, pop, size, clear, isEmpty, inspect, to_s.

In searching online I found various posts using this syntax to extract features of the Array class into a subclass:

Stack = Array.extract([
  :push,
  :pop,
  :size,
  :clear,
  :inspect,
  :to_s
])

s = Stack.new
s.push 1
s.push 2
s.push 3
s       # => [1, 2, 3]
s.pop   # => 3
s       # => [1, 2]

I would like to do something similar to this so my subclass of Array is restricted in what calls it can make, but it appears that the extract method is no longer in the Array class API.

Questions:

  1. This feature was removed for a reason, what is the detriment of something like this?
  2. How can achieve functionality similar to this using Ruby 1.9.3? Right now I am just delegating the calls that I need to the Array class, but all the other methods in the Array class can still be called on my Stack object, which I don’t want to allow.
  • 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-31T23:50:03+00:00Added an answer on May 31, 2026 at 11:50 pm

    Take this code, for example:

    class Stack
      def initialize
        @array = []
      end
    
      def push val
        @array.push val
      end
    
      def pop 
        @array.pop
      end
    end
    

    Here you have private instance var to which you delegate selected methods. Other methods cannot be called directly. The syntax can be sweetened and made more “rubesque” with some metaprogramming, but basic idea is as shown above.

    Of course, one can always get to that private var via instance_variable_get and there’s nothing you can do about it. This is Ruby!

    Make clean safe public interface. And if someone tries to meddle with inner parts and breaks something, it’s his problem.

    Update

    If you’re using ActiveSupport (which comes with Rails), then there’s a simpler way of doing this.

    # load ActiveSupport if not in Rails env
    require 'active_support/core_ext'
    
    class Stack
      def initialize
        @impl = []
      end
    
      # the "extract" part :)
      delegate :count, :push, :pop, to: :@impl
    end
    
    s = Stack.new
    s.push(3).push(4)
    s.count # => 2
    

    Or look at similar answer by @AndrewGrimm.

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

Sidebar

Related Questions

I need to use something like get_or_create() but the problem is that I have
I have a large codebase (written by me) that uses the Stack data structure.
I have some files that need to use by my application when it is
I need to use the mongo-storm library and Clojars doesn't have it . So
I have a timed page that I need to use to submit a form
We're developing a data heavy modular web application stack with java but have little
Because anything from C++ and beyond have nice constructs that handles a stack like
I need to use IBM Informix for my project where I have point coordinates
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I have a xsl file where i need to use parameters from an external

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.