Skip to content

Instantly share code, notes, and snippets.

View practicingruby's full-sized avatar

Gregory Brown practicingruby

View GitHub Profile
@practicingruby
practicingruby / 1_rcat_tests.rb
Last active September 27, 2015 15:47
Implementing a clone of UNIX cat in Ruby
# Task: Implement the rcat utility and get these tests to pass on a system
# which has the UNIX cat command present
# To see Gregory Brown's solution, see http://github.com/elm-city-craftworks/rcat
# Feel free to publicly share your own solutions
rrequire "open3"
working_dir = File.dirname(__FILE__)
gettysburg_file = "#{working_dir}/data/gettysburg.txt"
>> class A; end; A.new(:foo, :bar)
=> #<A:0x00000100b6f9a8>
>> RUBY_VERSION
=> "1.9.2"
>> class A; end; A.new(:foo, :bar)
ArgumentError: wrong number of arguments(2 for 0)
from (irb):1:in `initialize'
from (irb):1:in `new'
from (irb):1
# Looking to reduces rows of board game data to
# primitives (for eventual JSON output).
# each cell can be either unoccupied, or occupied by N
# armies of a given color. What data format do you prefer,
# and why?
#----------------------------------------------------------
# 1) nil indicates empty territory
[[:white, 2], nil, [:black, 3]]
Stack Wars is an abstract board game played on a two dimensional grid, similar
to a Chess or Go board. The game is currently still being play tested /
balanced, but the rules below should give you enough to try it out on your own.
== Board layout + setup
The board represents a battlefield that two players must
fight for control over, with the goal of invading each other's bases. The ASCII
representation below shows a standard 9x9 grid representing the battlefield.
# is this evil, weird, or awesome?
module ExtendedExtend
def extend(*args, &block)
if args.empty?
super(Module.new(&block))
else
super
end
end
# Given
Match /^I have entered (\d+) into the calculator$/ do |n|
@calculator ||= Calculator.new
@calculator.push(n.to_i)
end
# When
Match /^I press add$/ do
@result = @calculator.add
@practicingruby
practicingruby / addition.feature
Created November 1, 2011 14:08
Examples for Practicing Ruby 2.11
Feature: Addition
Scenario: Add two numbers
Given I have entered 70 into the calculator
And I have entered 50 into the calculator
When I press add
Then the result should be 120
@practicingruby
practicingruby / bmp_writer.rb
Last active December 18, 2023 12:29
bmp_writer.rb
# coding: binary
class BMP
class Writer
PIXEL_ARRAY_OFFSET = 54
BITS_PER_PIXEL = 24
DIB_HEADER_SIZE = 40
PIXELS_PER_METER = 2835 # 2835 pixels per meter is basically 72dpi
def initialize(width, height)
@practicingruby
practicingruby / bmp_reader.rb
Last active January 17, 2019 12:28
bmp_reader.rb
# coding: binary
class BMP
class Reader
PIXEL_ARRAY_OFFSET = 54
BITS_PER_PIXEL = 24
DIB_HEADER_SIZE = 40
def initialize(bmp_filename)
File.open(bmp_filename, "rb") do |file|
it "implements ROT" do
execute %w[
:foo
:bar
:baz
:foobar
:quux
ROT
]