Skip to content

Instantly share code, notes, and snippets.

View practicingruby's full-sized avatar

Gregory Brown practicingruby

View GitHub Profile

When access to an object's internals is truly necessary, it isn't practical to use composition based techniques. For example, consider the following mixin-based code which implements a memoization routine for caching method return values:

module Cached
  def cache(*method_names)
    method_names.each do |m|
      original = instance_method(m)
class ComposedCache
def initialize(target)
@target = target
end
# does not support caching with recursive calls (or calls to other cached methods)
def cache(*method_names)
method_names.each do |m|
results = {}
class ComposedCache
def initialize(target)
@target = target
end
def cache(*method_names)
method_names.each do |m|
original = @target.method(m)
results = {}
module Cached
def cache(*method_names)
method_names.each do |m|
original = instance_method(m)
results = {}
define_method(m) do |*a|
results[[m, a]] ||= original.bind(self).call(*a)
end
end
module MyApplication
Conditions = {}
Conditions[:near_christmas] = FuzzyLogic::Set.new(1) { |time|
# default output is zero
o = 0.0
# time has to be in december before or at the 24th
if time.month == 12 and time.day <= 24 then
# set is completly true when its 20th december or above
# from Alberto Fernandez-Capel(afcapel) for a practicing ruby article
require 'thread'
module Actor
module ClassMethods
def new(*args, &block)
Thread.current[:actor] = Proxy.new(super)
end
end
@practicingruby
practicingruby / puzzlenode1.lua
Last active December 11, 2015 04:18
Puzzlenode #4 in Lua
-- Implements the Robots vs. Lasers problem:
-- http://puzzlenode.com/puzzles/4-robots-vs-lasers
conveyors = {}
function conveyors.parse(filename)
local f = io.open(filename, "r")
local conveyors = {}
repeat
@practicingruby
practicingruby / idea.md
Last active December 10, 2015 22:49
Evidence based software development

I am considering starting up a community-funded project that is aimed at exploring software development methodologies and practices through the lens of evidence-based scientific studies. The purpose of this project would be to design, conduct, and report on real world experiments that attempt to answer questions that are relevant to everyday programmers.

This project would be subscriber-supported, but would follow a completely open publication model.

Subscribers would pay a small fee (maybe $5 to $10/month) and get all of the following benefits:

  • A monthly progress report summarizing all interesting activity on the project
  • Opportunities to read early drafts of articles
  • Opportunities to participate in pilot sessions for studies
  • Access to a members-only mailing list to discuss research activities with other supporters and with the project's maintainers.
require "open3"
Open3.popen3("cat") do |stdin, stdout|
stdin.puts("HAHAHAHHAA")
puts stdout.gets
end
@practicingruby
practicingruby / mrdi.md
Created December 19, 2012 22:29
Models, Roles, Decorators, and Interactions -- A modest proposal for a toned done version of DCI that isn't as janky as Concerns.

Models, Roles, Decorators, and Interactions

A modest alternative to DCI that might be worth further thought

One of the problems with advancing the discussion on DCI is that we lack a comparable alternative pattern that has the same goals, but favors a low ceremony approach. The closest thing we have to that is Rails concerns, but they are more like distant relatives of the DCI concepts rather than first cousins, and that makes comparisions between the two approaches not especially fruitful.

I am considering the idea of experimenting with my own paradigm that captures the intent and purity of DCI, but with the convenience of concerns. Please note that this is just the starting point of a conversation, it is NOT a promise of comercially available cold fusion or a cure for cancer. It's just a gist with an idea on it I'd like to hear your thoughts on.

What if we had a top-level topology that was split into Models, **Rol