Taken from this answer on StackOverflow
name = "Person"
attributes = [:name, :age]
class MyBaseClass; end
klass = Class.new(MyBaseClass) do
ATTRIBUTES = attributes
Taken from this answer on StackOverflow
name = "Person"
attributes = [:name, :age]
class MyBaseClass; end
klass = Class.new(MyBaseClass) do
ATTRIBUTES = attributes
module AppCallbacks
extend ActiveSupport::Concern
included do
include ActiveSupport::Callbacks
end
class_methods do
def define_app_callbacks(*callback_names)
poodr quotes
[Regarding refactoring and good design practices:] They are needed, not because the design is clear, but because it isn't. You don't have to know where you're going to use good design practices to get there. Good practices reveal design.
The code is not perfect, but in some ways it achieves a higher standard: it is good enough.
class Rx | |
class Subject | |
def initialize | |
@pipes = [] | |
end | |
def next(object) | |
threads = @pipes.map { |pipe| Thread.new { pipe.push object } } | |
threads.each(&:join) | |
end |
def process_sequence(sequence, last_robot_letter="B", robots={"B" => {position: 1, time: 0}, "O" => {position: 1, time: 0}}) | |
robot_letter = sequence.shift | |
current_robot_position = robots[robot_letter][:position] | |
current_robot_time = robots[robot_letter][:time] | |
next_robot_position = sequence.shift.to_i | |
next_robot_time = (current_robot_position - next_robot_position).abs + current_robot_time | |
last_robot_time = robots[last_robot_letter][:time] |