Skip to content

Instantly share code, notes, and snippets.

View practicingruby's full-sized avatar

Gregory Brown practicingruby

View GitHub Profile
@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])
class A
class << self
private
def foo
puts "bar"
end
end
class Person
alias_method :inspect, :to_s
def initialize(name, email)
@name = name
@email = email
end
def to_s
"#{@name} <#{@email}>"
module Assertive
FailedConstraint = Class.new(StandardError)
class << self
attr_accessor :enabled
end
def constraints
@constraints ||= []
end
class BoundedStack
OverflowError = Class.new(StandardError)
invariant { data.size <= limit }
def initialize(limit)
self.data = []
self.limit = limit
end
module Person
attr_accessor :email, :first_name, :last_name
end
class Employee
include Person
attr_accessor :role
end
module A
def a
end
end
module B
def b
end
end
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
def update(id, params)
# FIXME: This is not secure
sql = params.map { |k,v| "#{k} = #{v.inspect}" }.join(", ")
# BrokenRecord.database is just an instance of Sqlite::Database
BrokenRecord.database.execute %{
update #{@table_name}
set #{sql}
where id = #{id}
}
require "minitest/autorun"
require "delegate"
require "sqlite3"
describe "Mapping fields in results" do
before do
BrokenRecord.database = SQLite3::Database.new(":memory:")
BrokenRecord.database.execute %{