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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:59:45+00:00 2026-06-06T11:59:45+00:00

I’m creating a class that creates arrays. (Yes, I realize that’s already available in

  • 0

I’m creating a class that creates arrays. (Yes, I realize that’s already available in ruby. This is more of an exercise.) The class, SpecialArray, creates an array based on input that’s either given by the user when creating a new array or added to the array later. I’m having trouble connecting these two pieces of functionality. Creating an array works fine, but adding to it does not. I had thought that the output atrr_reader would hold the exiting array in memory, but now I’m not sure. Any idea how I can make this work?

    class SpecialArray
      attr_reader :input, :output

      def initialize(*input)
        @input = input.flatten
        @output = []
        generate_array
      end

      def generate_array
        input.each do |e|
          add(e)
        end
        output
      end

            #update start
    def numerical(element)
      element.class == Fixnum
    end

    def unique(element)
      output.include? element ? false : true
    end

    def valid_e(element)
      unique(element) && numerical(element)
    end

    def numerical(elment)
      element.class == Fixnum
    end

    def unique(element)
      output.include? element ? false : true
    end

    def valid_e(element)
      unique(element) && numerical(element)
    end
        #update end

      def add(element)
        unless valid_e(element) == false
          output.push(element)
        end
        output
      end

    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-06T11:59:46+00:00Added an answer on June 6, 2026 at 11:59 am

    Seems to work fine to me, both @input and @output contain the array I passed in:

    irb(main):032:0> a = SpecialArray.new(1,2,3,4,5,6,7);
    irb(main):034:0> puts a.inspect
    #<SpecialArray:0x296e7e8 @input=[1, 2, 3, 4, 5, 6, 7], 
                             @output=[1, 2, 3, 4, 5, 6, 7]>
    => nil
    

    EDIT Full Code

    irb(main):001:0> class SpecialArray
    irb(main):002:1>       attr_reader :input, :output
    irb(main):003:1>
    irb(main):004:1*       def initialize(*input)
    irb(main):005:2>         @input = input.flatten
    irb(main):006:2>         @output = []
    irb(main):007:2>         generate_array
    irb(main):008:2>       end
    irb(main):009:1>
    irb(main):010:1*       def generate_array
    irb(main):011:2>         input.each do |e|
    irb(main):012:3*           add(e)
    irb(main):013:3>         end
    irb(main):014:2>         output
    irb(main):015:2>       end
    irb(main):016:1>
    irb(main):017:1*         #...
    irb(main):018:1*
    irb(main):019:1*       def add(element)
    irb(main):020:2>         unless valid_e(element) == false
    irb(main):021:3>           output.push(element)
    irb(main):022:3>         end
    irb(main):023:2>         output
    irb(main):024:2>       end
    irb(main):025:1>
    irb(main):026:1*       def valid_e(e)
    irb(main):027:2>         return true;
    irb(main):028:2*       end
    irb(main):029:1>
    irb(main):030:1*         #...
    irb(main):031:1*     end
    => nil
    irb(main):032:0> a = SpecialArray.new(1,2,3,4,5,6,7);
    irb(main):034:0> puts a.inspect
    #<SpecialArray:0x296e7e8 @input=[1, 2, 3, 4, 5, 6, 7], 
                             @output=[1, 2, 3, 4, 5, 6, 7]>
    => nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.