Skip to content

Instantly share code, notes, and snippets.

View meaganewaller's full-sized avatar

Meagan Waller meaganewaller

View GitHub Profile
@meaganewaller
meaganewaller / shapes-2.rb
Created January 1, 2015 02:09
liskov substitution principle
shapes = [Square.new, Rectangle.new]
def set_rectangle_width(shapes)
shapes.each do |shape|
shape.width = 5
shape.height = 6
end
end
@meaganewaller
meaganewaller / 1.rb
Created January 1, 2015 02:06
Ruby arrays
>> vowels = [ "a", "e", "i", "o", "u"]
>> vowels[0]
=> "a"
>> vowels.size
=> 5
@meaganewaller
meaganewaller / example.rb
Created January 1, 2015 02:03
DATAMAPPER AUTOMIGRATE VS AUTOUPGRADE
require 'dm-core'
require 'dm-migrations'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'mysql://localhost/test')
class Person
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
@meaganewaller
meaganewaller / santa-2.rb
Created January 1, 2015 02:02
secret santa ruby quiz
(0..@santas.size - 1).each do |num|
if @santas[num][:assignee][:last_name] == @santas[num][:assignment][:last_name]
assign_santas
end
end
@meaganewaller
meaganewaller / changer.rb
Created January 1, 2015 02:01
coin changer kata
class CoinChanger
def initialize
@purse = Hash.new(0)
end
def make_change(amount)
if amount >= 25
change = amount % 25
@purse[:quarter] = amount/25
make_change(change)
@meaganewaller
meaganewaller / example.rb
Created January 1, 2015 02:00
the problem with datamapper
applicant = Applicant.new(:name => "Meagan")
if applicant.save
# applicant is valid & saved
else
applicant.errors.each do |error|
puts error
end
end
require 'etc'
class AccountProtectionProxy
def initialize(real_account, owner_name)
@subject = real_account
@owner_name = owner_name
end
def deposit(amount)
@meaganewaller
meaganewaller / british_text_adapter.rb
Created January 1, 2015 01:48
the adapter pattern
class BritishTextObjectAdapter < TextObject
def initialize(bto)
@bto = bto
end
def text
return @bto.string
end
def size_inches
@meaganewaller
meaganewaller / simple_writer.rb
Created January 1, 2015 01:45
The Decorator Pattern
w = SimpleWriter.new('out')
class << w
alias old_write_line write_line
def write_line(line)
old_write_line("#{Time.new}: #{line}")
end
end
@meaganewaller
meaganewaller / html_report.rb
Created January 1, 2015 01:42
template method pattern
#html_report
class HTMLReport < Report
def output_start
puts '<html>'
end
def output_head
puts ' <head>'
puts " <title>#{@title}</title>"
puts ' </head>'