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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:52:41+00:00 2026-06-03T19:52:41+00:00

Does anyone have a good example in Ruby of using a Composite of Commands?

  • 0

Does anyone have a good example in Ruby of using a Composite of Commands? It is a design pattern hybrid that I have seen mentioned in various Design Patterns literature which sounds quite powerful, but have not been able to find any interesting use cases or code.

  • 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-03T19:52:42+00:00Added an answer on June 3, 2026 at 7:52 pm

    Inspired by the general idea and the sample pattern implementations in this blog post, here’s a stab at what it might look like:

    class CompositeCommand
      def initialize(description, command, undo)
        @description=description; @command=command; @undo=undo
        @children = []
      end
      def add_child(child); @children << child; self; end
      def execute
        @command.call() if @command && @command.is_a?(Proc)
        @children.each {|child| child.execute}
      end
      def undo
        @children.reverse.each {|child| child.undo}
        @undo.call() if @undo && @undo.is_a?(Proc)
      end
    end
    

    And sample usage using the application of a software installer program:

    class CreateFiles < CompositeCommand
      def initialize(name)
        cmd = Proc.new { puts "OK: #{name} files created" }
        undo = Proc.new { puts "OK: #{name} files removed" }
        super("Creating #{name} Files", cmd, undo)
      end
    end
    
    class SoftwareInstaller
      def initialize; @commands=[]; end
      def add_command(cmd); @commands << cmd; self; end
      def install; @commands.each(&:execute); self; end
      def uninstall; @commands.reverse.each(&:undo); self end
    end
    
    installer = SoftwareInstaller.new
    installer.add_command(
      CreateFiles.new('Binary').add_child(
        CreateFiles.new('Library')).add_child(
        CreateFiles.new('Executable')))
    installer.add_command(
      CreateFiles.new('Settings').add_child(
        CreateFiles.new('Configuration')).add_child(
        CreateFiles.new('Preferences')).add_child(
        CreateFiles.new('Help')))
    installer.install # => Runs all commands recursively
    installer.uninstall
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Silverlight UnitTest framerwork does anyone have a good example have how
Does anyone have a good example of using the HeaderParser class in Python for
Does anyone have a good Drupal upgrade strategy for an install that is in
Does anyone have any good sources of information of using NHibernate with Sql Azure
Does anyone have a good tutorial on Java + Cucumber integration? I've begun using
Does anyone have a good example of a WPF or silverlight windows gadget?
Does anyone have a good example of how to do https over http (or
Does anyone have a good example of creating a WPF custom control or some
Does anyone have a good example for common error handling for service calls? I
DOes anyone have any good information with regards to when to log, I was

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.