Skip to content

Instantly share code, notes, and snippets.

View practicingruby's full-sized avatar

Gregory Brown practicingruby

View GitHub Profile
seacreature: Hi everyone, we'll get started in a just a couple minutes 05:32 PM
seacreature: We also have some big news: We are starting the transition to making Mendicant a fully public community / school: https://github.com/mendicant/mendicantuniversity.org/wiki/Mendicant-University-roadmap 05:33 PM
seacreature: drbrain: I'll start hitting you with questions in like two minutes, putting in a food order right now so I don't forget to eat dinner :) 05:35 PM
drbrain: seacreature: awesome 05:36 PM
seacreature: For everyone else, what we're about to do is have drbrain review some of my code and questions about test-driven development. 05:36 PM
seacreature: I am currently doing a 90 day self-study in TDD and as part of that I promised to try to fill in my own holes in understanding where possible 05:36 PM
seacreature: the task of working on a simple little game lately proved to be very annoying for me testing wise, and so that's what we'll be talking about today 05:37 PM
seacreature: if you're not
class Point
def self.random(distance_range, randomizer=Random.new)
angle = randomizer.rand(0..2*Math::PI)
length = randomizer.rand(distance_range)
x = length*Math.cos(angle)
y = length*Math.sin(angle)
point = new(x, y)
center = new(0, 0)
@practicingruby
practicingruby / testing.md
Created May 3, 2012 13:53
Questions for drbrain

QUESTION 1: Is there a better way to test that callbacks are being called?

  it "must trigger an event when the danger zone is reached" do
    alert = false

    game.on_event(:enter_region, :danger_zone) { alert = true }
    game.on_event(:leave_region, :danger_zone) { alert = false }

    refute alert, "should not enable alert before danger zone is reached"
require 'ray'
require 'io/console'
Ray.game "Test" do
register { add_hook :quit, method(:exit!) }
scene :square do
self.frames_per_second = 5
@sound = Ray::Sound.new("Beep.wav")
@sound.pos = [rand(20..40), rand(20..40),0]
Our beloved Ruby
1.1
1.2
1.8
1.9
1.10
1.10.A
1.11
2.1
>> class A
>> def initialize(&block)
>> block.call
>> end
>> def foo
>> puts "hi"
>> end
>> end
=> nil
>> A.new { foo }
x = "awesome"
begin
puts "A"
puts "B"
end if x == "awesome"
2012-04-02: Work on PuzzleNode "Secret Messages" problem
Link: https://github.com/sandal/puzzlenode-solutions/compare/9070...3b79
>> File.foreach("/usr/share/dict/words").find { |l| l =~ /^apple$/i }.chomp
=> "apple
Tempted to provide a fake dictionary file, but YAGNI? (could have been an
alternate way to deal with performance)
Time spent on test boilerplate noticeably tedious and boring. Hard to maintain

Refactoring

+ R1: A good test suite increases confidence that code changes won't cause 
+ unexpected side effects without being noticed.

+ R2: Tests reduce the cost of structural changes in later stages of a project 
+ because fewer bugs sneak into a system unnoticed.

- R3: Brittle tests can increase the cost of refactoring if they focus on 

DISCLAIMER: The examples in this article are Rails-based, and I'm only an intermediate Rails developer at best. The focus of this article is on general maintenance patterns rather than specific implementation details, but if you see something ugly in the code samples, please give me some feedback!

When I launched the Practicing Ruby journal back in August 2011, I was just as excited about designing my own publishing software as I was about working on new articles. After several unsuccessful attempts at using existing services, I had finally thrown in the towel and decided that for this journal to be what I wanted it to be, it needed to run on its own custom software.

For the first few weeks, I was adding new bits of functionality as quickly as Jordan Byron and I could code them up. However, by mid-October we had built a tool that was good enough for day-to-day use, and I was generally happy with our progress. The unfortunate consequence of rea