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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:16:12+00:00 2026-05-27T06:16:12+00:00

In order to call MyClass::empty().empty Do I need to implement an empty method in

  • 0
  1. In order to call MyClass::empty().empty
    Do I need to implement an empty method in side MyClass? What would I write inside it?

  2. Following, I am not sure how such a call could work: x = MyClass::empty().add("a").add("b")
    Would this be simply calling the add method if it is empty?

  • 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-27T06:16:13+00:00Added an answer on May 27, 2026 at 6:16 am

    What would you write inside it? What do you want it to do?

    You’d have a class method named empty that returns something having an empty method that either (a) took no parameters, or (b) had them defaulted.

    class MyClass
      def self.empty
        return WithEmpty.new # Or existing WithEmpty
      end
    end
    

    It also needs to return something that has an add method, and returns itself.

    class WithEmpty
      def initialize
        @a_list = []
      end
    
      def add x
        @a_list << x
        self
      end
    
      def empty
        @a_list.empty?
      end
    end
    

    It’d be easier/quicker if you described what you’re trying to do. It’s not immediately obvious.

    pry(main)> MyClass::empty()
    => #<WithEmpty:0xa01a090 @a_list=[]>
    pry(main)> MyClass::empty().empty
    => true
    pry(main)> MyClass::empty.empty
    => true
    pry(main)> MyClass::empty.add("hi").add("bye")
    => #<WithEmpty:0x9fc8d1c @a_list=["hi", "bye"]>
    

    Now you have no immediate way to get to the “inner” class’s list, unless you saved it in either MyClass (sketchy, since the methods are class, not instance, method), or you save the return value from the last bunch of chained methods.

    But if you save the instance, what’s the point of doing it through MyClass? Again, I can’t help but think you need to define what you’re actually trying to accomplish, rather than focusing on how you might accomplish it. What’s the goal?


    Firstly, there is no “add” method for an array. What you’re describing, as yet, makes no sense, unless you mean something like this:

    class MyClass
      def self.empty
        return MyClass.new
      end
    
      def initialize
        @arr = []
      end
    
      def empty
        @arr.empty?
      end
    
      def add x
        @arr << x
        self
      end
    end
    

    Then:

    pry(main)> MyClass.empty
    => #<MyClass:0x9aea390 @arr=[]>
    pry(main)> MyClass.empty.add("ohai").add("kthxbai")
    => #<MyClass:0x99f505c @arr=["ohai", "kthxbai"]>
    

    But again: you’re mixing class and instance methods in a way I don’t find sensical. How do you want to access the array? What’s the point of MyClass? Why not just use an array?

    pry(main)> a = []
    => []
    pry(main)> a.empty?
    => true
    pry(main)> a << "ohai" << "kthxbai"
    => ["ohai", "kthxbai"]
    pry(main)> 
    pry(main)> a.empty?
    => false
    

    Without any guidelines for what MyClass‘s purpose is, telling you a reasonable way to implement it is difficult, because without context, what you’ve shown doesn’t make a lot of sense.

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

Sidebar

Related Questions

In order to call some method just once in the app's lifecycle, not each
Do you use the alias method in order to add more ways to call
Order by descending is not working on LINQ to Entity In the following Query
Sometimes I need to provide a Context object to call specific functions, such as
Can I see in firebug profiler funcitons list in order those call? Do you
I'm programming WCF using the ChannelFactory which expects a type in order to call
I have a domain Aggregate, call it Order that contains a List of OrderLines.
When you call dict.values() the order of the returned items is dependent on the
How do I call a url in order to process the results? I have
I create a custom class which I call MyClass in a module MyModule module

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.