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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:50:35+00:00 2026-06-01T14:50:35+00:00

I am looking to do some method chaining. I have the following code: class

  • 0

I am looking to do some method chaining. I have the following code:

class MyClass

  attr_accessor :handler

  def do_a
    puts 'i just did a'
    self.handler = 'a'
    self
  end

  def do_b_if_a
    puts 'i just did b' if handler == 'a'
  end

 end

So the following works:

irb > test = MyClass.new
=> #<MyClass:0x007fa44ced9a70 @handler=nil>
irb > test.do_a
'i just did a'
irb > test.do_a.do_b_if_a
'i just did a'
'i just did b'

What I DONT want to work is when I call do_a the first time it sets the handler, which means now do_b_if_a can be called at any time. But I only want it to be called when it is chained with do_a, how do I do that?

  • 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-01T14:50:36+00:00Added an answer on June 1, 2026 at 2:50 pm

    I think you’re tracking state in the wrong place. You’d be better off with something similar to ActiveRecord’s query interface, for example:

    class MyClass
      attr_accessor :handler
    
      def do_a
        puts 'i just did a'
        with_handler 'a'
      end
    
      def do_b_if_a
        puts 'i just did b' if handler == 'a'
      end
    
    private
    
      def with_handler(h)
        o = dup
        o.handler = h
        o
      end
    
    end
    

    That way you always have an instance of MyClass but you have a throw-away copy that keeps track of its history. This is similar to cHao’s approach but it doesn’t need an extra decorator class as MyClass can decorate itself.

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

Sidebar

Related Questions

I'm just looking at NSString.h and wonder why some method declaration args use (NSString
I just changed my external broadcast receiver class to my service since..some android method
Suppose you have some method that could be made static, inside a non-static class.
Looking for some guidance on a WCF service I’m prototyping. I have a WCF
looking for some help with images referenced within the stylesheet. I have no problems
I was looking for some fast and efficient method do merge items in array.
I am looking through some text file for a certain string with the method.
I'm working with cocoa on the iPhone and I'm looking for some method like:
Assume someClass is a class defined in C# with some method int doSomething(void) ,
I'm looking for some ideas on how to compile Java code with some other

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.