Skip to content

Instantly share code, notes, and snippets.

View semmons99's full-sized avatar

Shane Emmons semmons99

View GitHub Profile
@semmons99
semmons99 / recipe.rb
Created February 29, 2012 14:43
minecraft crafting data modeling exercise
class Recipe
attr_reader :recipe
def initialize(recipe)
@recipe = recipe
standardize
end
private
@semmons99
semmons99 / results-1.8.7-p357.txt
Created February 16, 2012 18:03
strings: single vs. double quotes
$ ruby strings.rb [1.8.7-p357]
user system total real
double quote
assignment: 1.580000 0.000000 1.580000 ( 1.575944)
+: 8.750000 0.000000 8.750000 ( 8.746774)
<<: 9.660000 0.010000 9.670000 ( 9.668061)
interpolation: 8.870000 0.000000 8.870000 ( 8.869000)
single quote
assignment: 1.570000 0.000000 1.570000 ( 1.570190)
+: 8.580000 0.000000 8.580000 ( 8.581263)
module Foo
extend self
def initialize(*args)
super
end
end
class Bar
def initialize
C:\>irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'rdoc'
=> true
irb(main):003:0> puts $LOAD_PATH
C:/Dropbox/apps/Ruby-192-p180/lib/ruby/site_ruby/1.9.1
C:/Dropbox/apps/Ruby-192-p180/lib/ruby/site_ruby/1.9.1/i386-msvcrt
C:/Dropbox/apps/Ruby-192-p180/lib/ruby/site_ruby
C:/Dropbox/apps/Ruby-192-p180/lib/ruby/vendor_ruby/1.9.1
@semmons99
semmons99 / gist:1053857
Created June 29, 2011 13:42
bankers rounding
def round(n)
cents = n * BigDecimal("100")
remainder = cents % BigDecimal("1")
if remainder == BigDecimal("0")
n
elsif remainder < BigDecimal("0.5")
n.round(2, BigDecimal::ROUND_DOWN)
elsif remainder > BigDecimal("0.5")
n.round(2, BigDecimal::ROUND_UP)
module MyModule
extend self
attr_accessor :log_results
@log_results = true
def parse
if @log_results == true
puts "logging results"
module GitStats
module CLI
extend self
def parse(argv)
options = parse_options(argv)
end
private
module RobotFactory
def self.next_direction(factory_floor)
# same code as before, however, instead of printing "GO WEST"
# or "GO EAST" return those as strings.
end
private
def count_impacts
# same code as before
def bankers_round(amount)
scaled_amt = amount * BigDecimal("100")
if scaled_amt.frac > BigDecimal("0.5")
return scaled_amt.ceil / BigDecimal("100")
elsif scaled_amt.frac < BigDecimal("0.5")
return scaled_amt.floor / BigDecimal("100")
end
return scaled_amt.floor / BigDecimal("100") if scaled_amt.to_i.even?
class Rates
def initialize(rates_xml)
@rates = {}
@rates_xml = rates_xml
end
def rate(from, to)
@rates["#{from}:#{to}"] ||= derive_rate(from, to)
end