Skip to content

Instantly share code, notes, and snippets.

@tmikeschu
Last active May 4, 2020 07:38
Show Gist options
  • Save tmikeschu/ea25ad9b381b82f7302d653a512fc232 to your computer and use it in GitHub Desktop.
Save tmikeschu/ea25ad9b381b82f7302d653a512fc232 to your computer and use it in GitHub Desktop.
# page 2
=begin
Station>>computePart: aPart
^self multiplyPartTimesRate: aPart
Station>>multiplyPartTimesRate: aPart
^Part
amount: aPart amount * self rate
date: aPart date
=end
class Station
def compute_part(a_part)
multiply_part_times_rate(a_part)
end
def multiply_part_times_rate(a_part)
# where `rate` is probably an initialized attr on the Station instance
# where Part has .amount, #amount, and #date defined.
Part.amount(a_part.amount * rate, date: a_part.date)
end
end
# page 3 (refactored)
=begin
Part>>* aRate
^Part
amount: amount * aRate
date: date
Station>>computePart: aPart
^aPart * self rate
=end
class Part
def *(a_rate)
Part.amount(amount * a_rate, date: date)
end
end
class Station
def compute_part(a_part)
a_part * rate
end
end
# page 22, Composed Method
=begin
Controller>>controlActivity
self controlInitialize.
self controlLoop.
self controlTerminate.
=end
class Controller
def control_activity
# where each control_ reference is an instance method on Controller
control_initialize
control_loop
control_terminate
end
end
# page 24, Constructor Method
=begin
Point class>>r: radiusNumber theta: thetaNumber
^self
x: radiusNumber * thetaNumber cos
y: radiusNumber * thetaNumber sin
=end
class Point
def self.x(x_number, y: y_number)
# ...
end
def self.r(radius_number, theta: theta_number)
x(
(radius_number * thetaNumber).cos,
y: (radiusNumber * theta_number).sin
)
end
end
# page 25, Constructor Parameter Method
=begin
Point class>>x: xNumber y: yNumber
^self new
x: xNumber;
y: yNumber;
yourself
=end
class Point
def self.x(x_number, y: y_number)
self.new.x(x_number, y: y_number)
end
end
# refactored
=begin
Point class>>x: xNumber y: yNumber
^self new
setX: xNumber
y: yNumber
Point>>setX: xNumber y: yNumber
x := xNumber
y := yNumber
^self
=end
class Point
def self.x(x_number, y: y_number)
self.new.set_x(x_number, y: y_number)
end
def set_x(x_number, y: y_number)
@x = x_number
@y = y_number
end
end
# page 27, Shortcut Constructor Method
=begin
Number>>@ aNumber
^Point
x: self
y: aNumber
=end
class Number
def @(a_number)
Point.x(self, y: a_number)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment