Skip to content

Instantly share code, notes, and snippets.

View practicingruby's full-sized avatar

Gregory Brown practicingruby

View GitHub Profile
articles = BrokenRecord::Table.new(:name => "articles",
:db => BrokenRecord.database)
articles.insert(:body => "Hey joe", :title => "blah")
articles.delete(:id => 10)
articles.columns
(return array of hashes)
articles.all
(select * from articles), returns an array of hashes
module A
def a
end
end
module B
def b
end
end
module Person
attr_accessor :email, :first_name, :last_name
end
class Employee
include Person
attr_accessor :role
end
class BoundedStack
OverflowError = Class.new(StandardError)
invariant { data.size <= limit }
def initialize(limit)
self.data = []
self.limit = limit
end
module Assertive
FailedConstraint = Class.new(StandardError)
class << self
attr_accessor :enabled
end
def constraints
@constraints ||= []
end
class Person
alias_method :inspect, :to_s
def initialize(name, email)
@name = name
@email = email
end
def to_s
"#{@name} <#{@email}>"
class A
class << self
private
def foo
puts "bar"
end
end
@practicingruby
practicingruby / calc.rb
Created July 12, 2012 15:22
Code Golf RPN Calculator!
p((_=[gets.split,[],"+-**/"])[0].map{|__|__=~/[0-9\.]+/?_[1]<<__.to_f : _[2][__]?_[1]<<_[1].pop.send(__,_[1].pop):0}[0][0])
puts 'Hello, and welcome to seventh grade English.'
puts 'My name is Mrs. Crabbage. And your name is...?'
name = gets.chomp
# instead of name = 'jana' or 'Jana' do this...
if name == 'jana' || name == 'Jana'
puts 'Oh, YOU again. Please take a seat.'
end
# we usually use || instead of or, because it works
### LESSON 2: Refactoring is not redesign
https://github.com/mendicant/mendicantuniversity.org/wiki/TDD-study-session-%282012-May-03%29
code that needs redesign is not necessarily messy, just no longer correct
testing eases refactoring but has a complicated effect on redesign
http://devblog.avdi.org/2012/06/25/every-day-in-every-way/
A refactoring is an atomic and safe change that can be described in a sentence