repo stringlengths 5 92 | file_url stringlengths 80 287 | file_path stringlengths 5 197 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-04 15:37:27 2026-01-04 17:58:21 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/provider.rb | lib/vagrant-aws/provider.rb | require "log4r"
require "vagrant"
module VagrantPlugins
module AWS
class Provider < Vagrant.plugin("2", :provider)
def initialize(machine)
@machine = machine
end
def action(name)
# Attempt to get the action method from the Action class if it
# exists, otherwise return n... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/version.rb | lib/vagrant-aws/version.rb | module VagrantPlugins
module AWS
VERSION = '0.7.2'
end
end
| ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/errors.rb | lib/vagrant-aws/errors.rb | require "vagrant"
module VagrantPlugins
module AWS
module Errors
class VagrantAWSError < Vagrant::Errors::VagrantError
error_namespace("vagrant_aws.errors")
end
class FogError < VagrantAWSError
error_key(:fog_error)
end
class InternalFogError < VagrantAWSError
... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/plugin.rb | lib/vagrant-aws/plugin.rb | begin
require "vagrant"
rescue LoadError
raise "The Vagrant AWS plugin must be run within Vagrant."
end
# This is a sanity check to make sure no one is attempting to install
# this into an early Vagrant version.
if Vagrant::VERSION < "1.2.0"
raise "The Vagrant AWS plugin is only compatible with Vagrant 1.2+"
end... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/config.rb | lib/vagrant-aws/config.rb | require "vagrant"
require "iniparse"
module VagrantPlugins
module AWS
class Config < Vagrant.plugin("2", :config)
# The access key ID for accessing AWS.
#
# @return [String]
attr_accessor :access_key_id
# The ID of the AMI to use.
#
# @return [String]
attr_accesso... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action.rb | lib/vagrant-aws/action.rb | require "pathname"
require "vagrant/action/builder"
module VagrantPlugins
module AWS
module Action
# Include the built-in modules so we can use them as top-level things.
include Vagrant::Action::Builtin
def self.action_package
Vagrant::Action::Builder.new.tap do |b|
b.use Ca... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/util/elb.rb | lib/vagrant-aws/util/elb.rb | module VagrantPlugins
module AWS
module ElasticLoadBalancer
def register_instance(env, elb_name, instance_id)
env[:ui].info I18n.t("vagrant_aws.elb.registering", instance_id: instance_id, elb_name: elb_name), :new_line => false
elb = get_load_balancer(env[:aws_elb], elb_name)
unless... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/util/timer.rb | lib/vagrant-aws/util/timer.rb | module VagrantPlugins
module AWS
module Util
class Timer
# A basic utility method that times the execution of the given
# block and returns it.
def self.time
start_time = Time.now.to_f
yield
end_time = Time.now.to_f
end_time - start_time
... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/read_state.rb | lib/vagrant-aws/action/read_state.rb | require "log4r"
module VagrantPlugins
module AWS
module Action
# This action reads the state of the machine and puts it in the
# `:machine_state_id` key in the environment.
class ReadState
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_aw... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/warn_networks.rb | lib/vagrant-aws/action/warn_networks.rb | module VagrantPlugins
module AWS
module Action
class WarnNetworks
def initialize(app, env)
@app = app
end
def call(env)
if env[:machine].config.vm.networks.length > 0
env[:ui].warn(I18n.t("vagrant_aws.warn_networks"))
end
@app.cal... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/terminate_instance.rb | lib/vagrant-aws/action/terminate_instance.rb | require "log4r"
require "json"
module VagrantPlugins
module AWS
module Action
# This terminates the running instance.
class TerminateInstance
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_aws::action::terminate_instance")
end
... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/start_instance.rb | lib/vagrant-aws/action/start_instance.rb | require "log4r"
require 'vagrant/util/retryable'
require 'vagrant-aws/util/timer'
module VagrantPlugins
module AWS
module Action
# This starts a stopped instance.
class StartInstance
include Vagrant::Util::Retryable
def initialize(app, env)
@app = app
@logger... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/connect_aws.rb | lib/vagrant-aws/action/connect_aws.rb | require "fog"
require "log4r"
module VagrantPlugins
module AWS
module Action
# This action connects to AWS, verifies credentials work, and
# puts the AWS connection object into the `:aws_compute` key
# in the environment.
class ConnectAWS
def initialize(app, env)
@app ... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/message_will_not_destroy.rb | lib/vagrant-aws/action/message_will_not_destroy.rb | module VagrantPlugins
module AWS
module Action
class MessageWillNotDestroy
def initialize(app, env)
@app = app
end
def call(env)
env[:ui].info(I18n.t("vagrant_aws.will_not_destroy", name: env[:machine].name))
@app.call(env)
end
end
end... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/is_created.rb | lib/vagrant-aws/action/is_created.rb | module VagrantPlugins
module AWS
module Action
# This can be used with "Call" built-in to check if the machine
# is created and branch in the middleware.
class IsCreated
def initialize(app, env)
@app = app
end
def call(env)
env[:result] = env[:machine... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/run_instance.rb | lib/vagrant-aws/action/run_instance.rb | require "log4r"
require 'json'
require 'vagrant/util/retryable'
require 'vagrant-aws/util/timer'
module VagrantPlugins
module AWS
module Action
# This runs the configured instance.
class RunInstance
include Vagrant::Util::Retryable
def initialize(app, env)
@app = app
... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/message_not_created.rb | lib/vagrant-aws/action/message_not_created.rb | module VagrantPlugins
module AWS
module Action
class MessageNotCreated
def initialize(app, env)
@app = app
end
def call(env)
env[:ui].info(I18n.t("vagrant_aws.not_created"))
@app.call(env)
end
end
end
end
end
| ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/timed_provision.rb | lib/vagrant-aws/action/timed_provision.rb | require "vagrant-aws/util/timer"
module VagrantPlugins
module AWS
module Action
# This is the same as the builtin provision except it times the
# provisioner runs.
class TimedProvision < Vagrant::Action::Builtin::Provision
def run_provisioner(env, name, p)
timer = Util::Timer.... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/stop_instance.rb | lib/vagrant-aws/action/stop_instance.rb | require "log4r"
module VagrantPlugins
module AWS
module Action
# This stops the running instance.
class StopInstance
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_aws::action::stop_instance")
end
def call(env)
server... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/package_instance.rb | lib/vagrant-aws/action/package_instance.rb | require "log4r"
require 'vagrant/util/template_renderer'
require 'vagrant-aws/util/timer'
require 'vagrant/action/general/package'
module VagrantPlugins
module AWS
module Action
# This action packages a running aws-based server into an
# aws-based vagrant box. It does so by burning the associated
... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/is_stopped.rb | lib/vagrant-aws/action/is_stopped.rb | module VagrantPlugins
module AWS
module Action
# This can be used with "Call" built-in to check if the machine
# is stopped and branch in the middleware.
class IsStopped
def initialize(app, env)
@app = app
end
def call(env)
env[:result] = env[:machine... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/message_already_created.rb | lib/vagrant-aws/action/message_already_created.rb | module VagrantPlugins
module AWS
module Action
class MessageAlreadyCreated
def initialize(app, env)
@app = app
end
def call(env)
env[:ui].info(I18n.t("vagrant_aws.already_status", :status => "created"))
@app.call(env)
end
end
end
end... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/wait_for_state.rb | lib/vagrant-aws/action/wait_for_state.rb | require "log4r"
require "timeout"
module VagrantPlugins
module AWS
module Action
# This action will wait for a machine to reach a specific state or quit by timeout
class WaitForState
# env[:result] will be false in case of timeout.
# @param [Symbol] state Target machine state.
... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/elb_register_instance.rb | lib/vagrant-aws/action/elb_register_instance.rb | require 'vagrant-aws/util/elb'
module VagrantPlugins
module AWS
module Action
# This registers instance in ELB
class ElbRegisterInstance
include ElasticLoadBalancer
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_aws::action::elb_regist... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/read_ssh_info.rb | lib/vagrant-aws/action/read_ssh_info.rb | require "log4r"
module VagrantPlugins
module AWS
module Action
# This action reads the SSH info for the machine and puts it into the
# `:machine_ssh_info` key in the environment.
class ReadSSHInfo
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("va... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
mitchellh/vagrant-aws | https://github.com/mitchellh/vagrant-aws/blob/43e7ec2eb425fdf0223910522b6e91a92329f00f/lib/vagrant-aws/action/elb_deregister_instance.rb | lib/vagrant-aws/action/elb_deregister_instance.rb | require 'vagrant-aws/util/elb'
module VagrantPlugins
module AWS
module Action
# This registers instance in ELB
class ElbDeregisterInstance
include ElasticLoadBalancer
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant_aws::action::elb_dere... | ruby | MIT | 43e7ec2eb425fdf0223910522b6e91a92329f00f | 2026-01-04T15:54:22.385766Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/main.rb | proxy/main.rb | require_relative 'bank_account'
require_relative 'bank_account_proxy'
require_relative 'virtual_account_proxy'
# Directly accessing BankAccount
account = BankAccount.new(100)
account.deposit(50)
account.withdraw(10)
puts account.balance
# Protection proxy
protection_proxy = BankAccountProxy.new(account)
protection_pr... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/virtual_account_proxy.rb | proxy/virtual_account_proxy.rb | class VirtualAccountProxy
def initialize(&creation_block)
@creation_block = creation_block
end
def deposit(amount)
subject.deposit(amount)
end
def withdraw(amount)
subject.withdraw(amount)
end
def balance
subject.balance
end
def subject
@subject ||= @creation_block.call
end
e... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/bank_account.rb | proxy/bank_account.rb | class BankAccount
attr_reader :balance
def initialize(starting_balance = 0)
@balance = starting_balance
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
@balance -= amount
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/account_protection_proxy.rb | proxy/account_protection_proxy.rb | require 'etc'
class AccountProtectionProxy
def initialize(real_account, owner_name)
@subject = real_account
@owner_name = owner_name
end
def deposit(amount)
check_access
@subject.deposit(amount)
end
def withdraw(amount)
check_access
@subject.withdraw(amount)
end
def balance
... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/bank_account_proxy.rb | proxy/bank_account_proxy.rb | class BankAccountProxy
def initialize(real_object)
@real_object = real_object
end
def balance
@real_object.balance
end
def deposit(amount)
@real_object.deposit(amount)
end
def withdraw(amount)
@real_object.withdraw(amount)
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/remote_proxy.rb | proxy/remote_proxy.rb | require 'soap/wsdlDriver'
wsdl_url = 'http://www.webservicex.net/WeatherForecast.asmx?WSDL'
proxy = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver
weather_info = proxy.GetWeatherByZipCode('ZipCode' => '19128')
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/using_messages/main.rb | proxy/using_messages/main.rb | require_relative '../bank_account'
require_relative 'account_proxy'
require_relative 'account_protection_proxy'
require_relative 'virtual_proxy'
proxy = AccountProxy.new(BankAccount.new(100))
proxy.deposit(25)
proxy.withdraw(50)
puts "account balance is now: #{proxy.balance}"
array = VirtualProxy.new { [] }
array << ... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/using_messages/virtual_proxy.rb | proxy/using_messages/virtual_proxy.rb | # Generic virtual proxy
class VirtualProxy
def initialize(&creation_block)
@creation_block = creation_block
end
def method_missing(name, *args)
subject.send(name, *args)
end
def to_s
subject.to_s
end
def subject
@subject ||= @creation_block.call
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/using_messages/account_proxy.rb | proxy/using_messages/account_proxy.rb | class AccountProxy
def initialize(real_account)
@subject = real_account
end
def method_missing(name, *args)
puts "Delegating #{name} message to subject."
@subject.send(name, *args)
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/proxy/using_messages/account_protection_proxy.rb | proxy/using_messages/account_protection_proxy.rb | require 'etc'
class AccountProtectionProxy
def initialize(real_account, owner_name)
@subject = real_account
@owner_name = owner_name
end
def method_missing(name, *args)
check_access
@subject.send(name, *args)
end
def check_access
if Etc.getlogin != @owner_name
raise "Illegal acces... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/main.rb | composite/main.rb | require_relative 'task'
require_relative 'composite_task'
require_relative 'make_cake_task'
require_relative 'make_batter_task'
require_relative 'add_dry_ingredients_task'
require_relative 'add_liquids_task'
require_relative 'mix_task'
require_relative 'fill_pan_task'
require_relative 'bake_task'
require_relative 'f... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/task.rb | composite/task.rb | class Task
attr_accessor :parent
attr_reader :name
def initialize(name)
@name = name
end
def time_required
0
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/add_liquids_task.rb | composite/add_liquids_task.rb | class AddLiquidsTask < Task
def initialize
super('Add liquid ingredients.')
end
def time_required
1.5 # 1.5 minutes to add liquids
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/add_dry_ingredients_task.rb | composite/add_dry_ingredients_task.rb | class AddDryIngredientsTask < Task
def initialize
super('Add dry ingredients')
end
def time_required
1 # 1 minute to add flour and sugar
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/composite_task.rb | composite/composite_task.rb | class CompositeTask < Task
def initialize(name)
super(name)
@sub_tasks = []
end
def add_sub_task(task)
@sub_tasks << task
task.parent = self
end
alias << add_sub_task
def remove_sub_task(task)
@sub_tasks.delete(task)
task.parent = nil
end
def [](index)
@sub_tasks[index]
... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/bake_task.rb | composite/bake_task.rb | class BakeTask < Task
def initialize
super('Bake the cake.')
end
def time_required
42 # 42 minutes
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/fill_pan_task.rb | composite/fill_pan_task.rb | class FillPanTask < Task
def initialize
super('Fill the pan with cake batter.')
end
def time_required
2.5 # 3 minutes
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/frost_task.rb | composite/frost_task.rb | class FrostTask < Task
def initialize
super('Put frosting on the cake.')
end
def time_required
3 # 3 minutes
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/make_cake_task.rb | composite/make_cake_task.rb | class MakeCakeTask < CompositeTask
def initialize
super('Make cake')
@sub_tasks = []
self << MakeBatterTask.new
self << FillPanTask.new
self << BakeTask.new
self << FrostTask.new
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/mix_task.rb | composite/mix_task.rb | class MixTask < Task
def initialize
super('Mix that batter up!')
end
def time_required
3 # Mix for 3 minutes
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/make_batter_task.rb | composite/make_batter_task.rb | class MakeBatterTask < CompositeTask
def initialize
super('Make batter')
@sub_tasks = []
add_sub_task AddDryIngredientsTask.new
add_sub_task AddLiquidsTask.new
add_sub_task MixTask.new
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/tree/main.rb | composite/tree/main.rb | require_relative 'node'
require_relative 'task'
make_cake = Task.new('Make cake')
make_batter = Task.new('Make Batter')
make_batter.add_child Task.new('Add Dry Ingredients', 1)
make_batter.add_child Task.new('Add Wet Ingredients', 1.5)
make_batter.add_child Task.new('Mix', 3)
make_cake.add_child make_batter
make_cak... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/tree/task.rb | composite/tree/task.rb | class Task < Node
def initialize(name, time_required = nil)
super(name)
@time_required = time_required
end
def time_required
if leaf?
@time_required
else
children.reduce(0.0) { |time, task| time += task.time_required }
end
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/composite/tree/node.rb | composite/tree/node.rb | class Node
attr_accessor :name, :parent
attr_reader :children
def initialize(name)
@name = name
@children = []
end
def add_child(node)
@children << node
node.parent = self
end
alias << add_child
def remove_child(node)
@children.delete node
end
def [](index)
@children[ind... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/builder/main.rb | builder/main.rb | require_relative 'cpus'
require_relative 'drive'
require_relative 'motherboard'
require_relative 'computer'
require_relative 'computer_builder'
builder = ComputerBuilder.new
builder.turbo
builder.display(:lcd)
builder.add_cd
builder.add_dvd(true)
builder.add_hard_disk(100_000)
# manufacture 10 computers using the bui... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/builder/drive.rb | builder/drive.rb | class Drive
attr_reader :type, :size, :writable
def initialize(type, size, writable)
@type = type
@size = size
@writable = writable
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/builder/computer.rb | builder/computer.rb | class Computer
attr_accessor :display, :motherboard, :drives
def initialize(display = :crt, motherboard = Motherboard.new, drives = [])
@display = display
@motherboard = motherboard
@drives = drives
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/builder/computer_builder.rb | builder/computer_builder.rb | class ComputerBuilder
attr_reader :computer
def initialize
@computer = Computer.new
end
def turbo(_has_turbo_cpu = true)
@computer.motherboard.cpu = TurboCPU.new
end
def display(display)
@computer.display = display
end
def memory_size(size_in_mb)
@computer.motherboard.memory_size = s... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/builder/cpus.rb | builder/cpus.rb | class CPU
# cpu stuff...
end
class BasicCPU < CPU
# basic cpu stuff...
end
class TurboCPU < CPU
# trubo fast cpu stuff...
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/builder/motherboard.rb | builder/motherboard.rb | class Motherboard
attr_accessor :cpu, :memory_size
def initialize(cpu = BasicCPU.new, memory_size = 1000)
@cpu = cpu
@memory_size = memory_size
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/main.rb | observer/main.rb | require_relative 'subject'
require_relative 'employee'
require_relative 'payroll'
require_relative 'tax_man'
fred = Employee.new('Fred Flintstone', 'Crane Operator', 30_000)
payroll = Payroll.new
fred.add_observer(payroll)
tax_man = TaxMan.new
fred.add_observer(tax_man)
# Give Fred a raise
fred.salary = 35_000
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/employee.rb | observer/employee.rb | class Employee
include Subject
attr_reader :name, :title
attr_reader :salary
def initialize(name, title, salary)
super()
@name = name
@title = title
@salary = salary
end
def salary=(new_salary)
@salary = new_salary
notify_observers
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/tax_man.rb | observer/tax_man.rb | class TaxMan
def update(changed_employee)
puts "Send #{changed_employee.name} a new tax bill!"
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/subject.rb | observer/subject.rb | # require 'forwardable'
module Subject
# extend Forwardable
# def_instance_delegator :@observers, :<<, :add_observer
# def_instance_delegator :@observers, :delete, :delete_observer
def initialize
@observers = []
end
def add_observer(observer)
@observers << observer
end
def delete_observer(o... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/payroll.rb | observer/payroll.rb | class Payroll
def update(changed_employee)
puts "Cut a new check for #{changed_employee.name}!"
puts "His salary is now #{changed_employee.salary}!"
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/using_stdlib_observable/main.rb | observer/using_stdlib_observable/main.rb | require_relative 'employee'
require_relative 'payroll'
require_relative 'tax_man'
fred = Employee.new('Fred Flintstone', 'Crane Operator', 30_000)
payroll = Payroll.new
fred.add_observer(payroll)
tax_man = TaxMan.new
fred.add_observer(tax_man)
# Give Fred a raise
fred.salary = 35_000
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/using_stdlib_observable/Payroll.rb | observer/using_stdlib_observable/Payroll.rb | class Payroll
def update(changed_employee)
puts "Cut a new check for #{changed_employee.name}!"
puts "His salary is now #{changed_employee.salary}!"
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/using_stdlib_observable/Subject.rb | observer/using_stdlib_observable/Subject.rb | module Subject
def initialize
@observers = []
end
def add_observer(observer)
@observers << observer
end
def delete_observer(observer)
@observers.delete(observer)
end
def notify_observers
@observers.each do |observer|
observer.update(self)
end
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/using_stdlib_observable/tax_man.rb | observer/using_stdlib_observable/tax_man.rb | class TaxMan
def update(changed_employee)
puts "Send #{changed_employee.name} a new tax bill!"
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/using_stdlib_observable/Employee.rb | observer/using_stdlib_observable/Employee.rb | require 'observer'
class Employee
include Observable
attr_reader :name, :title
attr_reader :salary
def initialize(name, title, salary)
super()
@name = name
@title = title
@salary = salary
end
def salary=(new_salary)
changed # Sets boolean flag that object has changed to true
@sal... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/using_code_blocks/main.rb | observer/using_code_blocks/main.rb | require_relative 'subject'
require_relative '../employee'
fred = Employee.new('Fred Flintstone', 'Crane Operator', 30_000)
fred.add_observer do |changed_employee|
puts "Cut a new check for #{changed_employee.name}!"
puts "His salary is now #{changed_employee.salary}!"
end
fred.salary = 35_000
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/observer/using_code_blocks/Subject.rb | observer/using_code_blocks/Subject.rb | module Subject
def initialize
@observers = []
end
def add_observer(&observer)
@observers << observer
end
def delete_observer(observer)
@observers.delete(observer)
end
def notify_observers
@observers.each do |observer|
observer.call(self)
end
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/singleton/main.rb | singleton/main.rb | require_relative 'simple_logger'
logger = SimpleLogger.instance
puts logger.level
SimpleLogger.instance.info('Computer wins chess game.')
SimpleLogger.instance.warning('AE-35 hardware failure predicted.')
SimpleLogger.instance.error('HAL-9000 malfunction, take emergency action!')
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/singleton/simple_logger.rb | singleton/simple_logger.rb | require 'singleton'
class SimpleLogger
include Singleton
attr_accessor :level
ERROR = 1
WARNING = 2
INFO = 3
def initialize
@log = File.open('log.txt', 'w')
@level = WARNING
end
## the Singleton module overrides the following code
# @@instance = self.new
#
# def self.instance
# @@... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/singleton/Logger.rb | singleton/Logger.rb | class Logger
attr_reader :log
def initialize
@log = File.open('log.txt', 'a')
end
@@instance = new
def self.instance
@@instance
end
# def log(msg)
# @log.puts(msg)
# end
private_class_method :new
end
Logger.instance.log
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/singleton/class_as_singleton/main.rb | singleton/class_as_singleton/main.rb | require_relative 'class_based_logger'
ClassBasedLogger.level = ClassBasedLogger::INFO
ClassBasedLogger.info('Computer wins chess game.')
ClassBasedLogger.warning('AE-35 hardware failure predicted.')
ClassBasedLogger.error('HAL-9000 malfunction, take emergency action!')
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/singleton/class_as_singleton/class_based_logger.rb | singleton/class_as_singleton/class_based_logger.rb | class ClassBasedLogger
ERROR = 1
WARNING = 2
INFO = 3
@@level = WARNING
# Lazily instantiate @@log
def self.log
@@log ||= File.open('log.txt', 'w')
end
def self.error(msg)
log.puts("Error: #{msg}")
log.flush
end
def self.warning(msg)
log.puts("Warning: #{msg}") if @@level >= WARN... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/singleton/module_as_singleton/main.rb | singleton/module_as_singleton/main.rb | require_relative 'module_based_logger'
ModuleBasedLogger.level = ModuleBasedLogger::INFO
ModuleBasedLogger.info('Computer wins chess game.')
ModuleBasedLogger.warning('AE-35 hardware failure predicted.')
ModuleBasedLogger.error('HAL-9000 malfunction, take emergency action!')
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/singleton/module_as_singleton/module_based_logger.rb | singleton/module_as_singleton/module_based_logger.rb | module ModuleBasedLogger
ERROR = 1
WARNING = 2
INFO = 3
@@level = WARNING
# Lazily instantiate @@log
def self.log
@@log ||= File.open('log.txt', 'w')
end
def self.error(msg)
log.puts("Error: #{msg}")
log.flush
end
def self.warning(msg)
log.puts("Warning: #{msg}") if @@level >= WA... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/internal_iterator/main.rb | iterator/internal_iterator/main.rb | # a = [10, 20, 30]
# require './for_each_element'
# for_each_element(a) { |element|
# puts "The element is #{element}"
# }
# a.each { |element|
# puts "The element is #{element}"
# }
a = %w(joe sam george)
# the 'all' method is provided to Array by the Enumerable module
puts a.all? { |element| element.length < 4... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/internal_iterator/for_each_element.rb | iterator/internal_iterator/for_each_element.rb | def for_each_element(array)
# Make a deep copy the array so that changes to the original array
# wont effect the iterator
array_copy = array.clone
i = 0
while i < array_copy.length
yield array_copy[i]
i += 1
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/external_iterator/main.rb | iterator/external_iterator/main.rb | require_relative 'array_iterator'
i = ArrayIterator.new(%w(red green blue))
puts "item: #{i.next_item}" while i.has_next?
i = ArrayIterator.new('abc')
puts("item: #{i.next_item}") while i.has_next?
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/external_iterator/array_iterator.rb | iterator/external_iterator/array_iterator.rb | class ArrayIterator
def initialize(array)
# Make a deep copy the array so that changes to the original array
# wont effect the iterator
@array = array.clone
@index = 0
end
def has_next?
@index < @array.length
end
# def item
# @array[@index]
# end
def next_item
value = @array... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/external_iterator/merge.rb | iterator/external_iterator/merge.rb | def merge(array1, array2)
merged = []
iterator1 = ArrayIterator.new(array1)
iterator2 = ArrayIterator.new(array2)
while iterator1.has_next? && iterator2.has_next?
merged << if iterator1.item < iterator2.item
iterator1.next_item
else
iterator2.next_item
... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/enumerable/main.rb | iterator/enumerable/main.rb | require_relative 'account'
require_relative 'portfolio'
portfolio = Portfolio.new
portfolio.add_account Account.new('red', 2000)
portfolio.add_account Account.new('blue', 1000)
portfolio.add_account Account.new('green', 500)
puts portfolio.any? { |account| account.balance > 2000 }
puts portfolio.all? { |account| acco... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/enumerable/account.rb | iterator/enumerable/account.rb | class Account
attr_accessor :name, :balance
def initialize(name, balance)
@name = name
@balance = balance
end
def <=>(other_account)
balance <=> other_account.balance
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/iterator/enumerable/portfolio.rb | iterator/enumerable/portfolio.rb | class Portfolio
include Enumerable
def initialize
@accounts = []
end
def each(&block)
@accounts.each(&block)
end
def add_account(account)
@accounts << account
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/commands/main.rb | commands/main.rb | require_relative 'command'
require_relative 'composite_command'
require_relative 'copy_file'
require_relative 'create_file'
require_relative 'delete_file'
cmds = CompositeCommand.new
cmds.add_command CreateFile.new('file1.txt', "hello world\n")
cmds.add_command CopyFile.new('file1.txt', 'file2.txt')
cmds.add_command D... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/commands/command.rb | commands/command.rb | class Command
attr_reader :description
def initialize(description)
@description = description
end
def execute
raise NotImplementedError
end
def unexecute
raise NotImplementedError
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/commands/copy_file.rb | commands/copy_file.rb | require 'fileutils'
class CopyFile < Command
def initialize(source, target)
super "Copy file: #{source} to #{target}"
@source = source
@target = target
end
def execute
@contents = File.read(@target) if File.exist?(@target)
FileUtils.copy(@source, @target)
end
def unexecute
if @conte... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/commands/delete_file.rb | commands/delete_file.rb | require 'fileutils'
class DeleteFile < Command
def initialize(path)
super "Delete file: #{path}"
@path = path
end
def execute
@contents = File.read(@path) if File.exist?(@path)
File.delete(@path)
end
def unexecute
if @contents
f = File.open(@path, 'w')
f.write(@contents)
... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/commands/create_file.rb | commands/create_file.rb | require 'fileutils'
class CreateFile < Command
def initialize(path, contents)
super "Create file: #{path}"
@path = path
@contents = contents
end
def execute
f = File.open(@path, 'w')
f.write @contents
f.close
end
def unexecute
File.delete(@path)
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/commands/composite_command.rb | commands/composite_command.rb | class CompositeCommand < Command
def initialize
@commands = []
end
def add_command(cmd)
@commands << cmd
end
def execute
@commands.each(&:execute)
end
def unexecute
@commands.reverse_each(&:unexecute)
end
def description
description = ''
@commands.each { |cmd| description +... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/main.rb | decorator/main.rb | # ------------------------------------------------------
# Naive implementation
# require_relative 'enhanced_writer'
# writer = EnhancedWriter.new('out.txt')
# writer.write_line('A plain line')
# writer.checksumming_write_line('A line with checksum')
# puts("Checksum is #{writer.check_sum}")
# writer.timestamping_... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/numbering_writer.rb | decorator/numbering_writer.rb | class NumberingWriter < WriterDecorator
def initialize(real_writer)
super(real_writer)
@line_number = 1
end
def write_line(line)
@real_writer.write_line("#{@line_number}: #{line}")
@line_number += 1
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/simple_writer.rb | decorator/simple_writer.rb | class SimpleWriter
def initialize(path)
@file = File.open(path, 'w')
end
def write_line(line)
@file.print(line)
@file.print("\n")
end
def pos
@file.pos
end
def rewind
@file.rewind
end
def close
@file.close
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/time_stamping_writer.rb | decorator/time_stamping_writer.rb | class TimeStampingWriter < WriterDecorator
def write_line(line)
@real_writer.write_line("#{Time.new}: #{line}")
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/check_summing_writer.rb | decorator/check_summing_writer.rb | class CheckSummingWriter < WriterDecorator
attr_reader :check_sum
def initialize(real_writer)
@real_writer = real_writer
@check_sum = 0
end
def write_line(line)
line.each_byte { |byte| @check_sum = (@check_sum + byte) % 256 }
@check_sum += "\n".ord % 256
@real_writer.write_line(line)
end... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/enhanced_writer.rb | decorator/enhanced_writer.rb | class EnhancedWriter
attr_reader :check_sum
def initialize(path)
@file = File.open(path, 'w')
@check_sum = 0
@line_number = 1
end
def write_line(line)
@file.print(line)
@file.print("\n")
end
def checksumming_write_line(data)
data.each_byte { |byte| @check_sum = (@check_sum + byte)... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/writer_decorator.rb | decorator/writer_decorator.rb | # WriterDecorator is an 'abstract' decorators class for SimpleWriter
# it contains generic code that can be overriden by the decorators
require 'forwardable'
class WriterDecorator
extend Forwardable
def_instance_delegators :@real_writer, :write_line, :pos, :rewind, :close
def initialize(real_writer)
@real_... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/using_modules/main.rb | decorator/using_modules/main.rb | require_relative '../simple_writer'
require_relative 'decorator_modules'
w = SimpleWriter.new('out.txt')
w.extend(NumberingWriter)
w.extend(TimeStampingWriter)
w.write_line('hello')
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/decorator/using_modules/decorator_modules.rb | decorator/using_modules/decorator_modules.rb | module TimeStampingWriter
def write_line(line)
super("#{Time.new}: #{line}")
end
end
module NumberingWriter
attr_reader :line_number
def write_line(line)
@line_number ||= 1
super("#{@line_number}: #{line}")
@line_number += 1
end
end
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/template_method/main.rb | template_method/main.rb | require_relative 'html_report'
require_relative 'plain_text_report'
report = HTMLReport.new
report.output_report
report = PlainTextReport.new
report.output_report
| ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
design-patterns-in-ruby/design-patterns-in-ruby | https://github.com/design-patterns-in-ruby/design-patterns-in-ruby/blob/8464db28f24de3d666d7da5da51f84d8a27441e6/template_method/report.rb | template_method/report.rb | class Report
def initialize
@title = 'Monthly Report'
@text = ['Things are going', 'really, really well.']
end
def output_report
output_start
output_head
output_body_start
@text.each { |line| output_line(line) }
output_body_end
output_end
end
def output_start
end
def ou... | ruby | CC0-1.0 | 8464db28f24de3d666d7da5da51f84d8a27441e6 | 2026-01-04T15:54:29.970168Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.