This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Chapter08 | |
| module ReplaceTypeCodeWithStateOrStrategy | |
| class RigidMountainBike | |
| def initialize(params) | |
| @tyre_width = params[:tyre_width] | |
| end | |
| def off_road_ability | |
| @tyre_width * MountainBike::TYRE_WIDTH_FACTOR | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Yet another version with suggestions from workmad3 :-) | |
| # This time, pass in the keyword args into the block. | |
| # This means duplicating the definition of valid params, but | |
| # also means you get normal local variables in the method body. | |
| module DefK | |
| # NOTE: params is for syntactic sugar, and has no bearing on the | |
| # number of arguments you can call | |
| def def_k(method_name, *params, kparams, &method_body) | |
| define_method method_name do |*args| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def make_me_an_animal_class(name, property) | |
| klass = Class.new | |
| klass.class_eval do | |
| define_method(:initialize) do |attribute| | |
| instance_variable_set("@#{property}", attribute) | |
| end | |
| define_method(property) do | |
| instance_variable_get("@#{property}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArduinoInterpreter | |
| def initialize(setup_block, tick_block) | |
| @setup_block, @tick_block = setup_block, tick_block | |
| end | |
| def serial_begin | |
| puts "Initialising serial port..." | |
| end | |
| def serial_write(data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| :: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (f n) | |
| (cond ((= n 1) 1) | |
| ((= n 2) 2) | |
| ((= n 3) 3) | |
| (else (+ (f (- n 1)) | |
| (* 2 (f (- n 2))) | |
| (* 3 (f (- n 3)))))) | |
| ) | |
| (define (g n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (expect actual expectation expected) | |
| (display "• ") | |
| (display actual) | |
| (display " should equal ") | |
| (display expected) | |
| (display ": ") | |
| (expectation actual expected) | |
| (display "pass\n") | |
| ) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; Slow! The (next) "optimisation" slows it down | |
| (define (find-divisor n test-divisor) | |
| (define (next n) | |
| (if (= n 2) | |
| 3 | |
| (+ n 2))) | |
| (cond ((> (square test-divisor) n) n) | |
| ((divides? test-divisor n) test-divisor) | |
| (else (find-divisor n (next test-divisor))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (expect actual expectation expected) | |
| (display "• ") | |
| (display actual) | |
| (display " should equal ") | |
| (display expected) | |
| (display ": ") | |
| (expectation actual expected) | |
| (display "pass\n") | |
| ) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define (expect actual expectation expected) | |
| (display "• ") | |
| (display actual) | |
| (display " should equal ") | |
| (display expected) | |
| (display ": ") | |
| (expectation actual expected) | |
| (display "pass\n") | |
| ) | |