Skip to content

Instantly share code, notes, and snippets.

View shawn42's full-sized avatar

Shawn Anderson shawn42

  • Cisco
  • Holland, MI
View GitHub Profile
@shawn42
shawn42 / group_by.rb
Created September 4, 2012 11:17
Piece Piece map, aggregate, group-by
# Using group-by
# no extra step definition is required
PiecePipe::Pipeline.new.
source([{region: region}]).
step(FetchPowerPlantsByRegion).
group_by(:region).
to_enum
@shawn42
shawn42 / behavior_dsl.rb
Created May 12, 2012 16:17
proposed behavior dsl
# CURRENT
has_behavior animated: {:frame_update_time => 900}
has_behavior collidable: {shape: :polygon, cw_local_points: [[4,4],[44,4],[44,44],[4,44]]}
has_behavior projectile: {speed: 0.01, direction: vec2(1,0)}
has_behavior :reversable_direction
has_behavior increasing_speed: { accel: 0.001 }
has_behavior drops: { fall_amount: 25}
has_behavior shooter: {shoots: :alien_missile, direction: vec2(0,-1)}
@shawn42
shawn42 / omg_aliens.rb
Created May 11, 2012 18:17
The guts of omg_aliens
# src/actors/alien.rb
define_actor :alien do
has_attributes action: :march, view: :graphical_actor_view
has_behaviors do
animated frame_update_time: 900
collidable shape: :polygon, cw_local_points: [[4,4],[44,4],[44,44],[4,44]]
projectile speed: 0.01, direction: vec2(1,0)
reversable_direction
increasing_speed accel: 0.001
@shawn42
shawn42 / broken_64_bit_flag.rb
Created May 4, 2012 20:13
vendor_fix_ruby_motion
if !File.exist?(bs_file) or headers.any? { |h| File.mtime(h) > File.mtime(bs_file) }
includes = headers.map { |p| "-I" + File.dirname(p) }.uniq.join(' ')
# ! gen_bridge_metadata DOES NOT support --no-64-bit
#sh "/usr/bin/gen_bridge_metadata --format complete --no-64-bit --cflags \"-I. #{includes}\" #{headers.join(' ')} -o \"#{bs_file}\""
sh "/usr/bin/gen_bridge_metadata --format complete --cflags \"-I. #{includes}\" #{headers.join(' ')} -o \"#{bs_file}\""
end
bs_files << bs_file
@shawn42
shawn42 / using.coffee
Created March 1, 2012 14:37
"using" in coffeescript
window.using = (namespaces..., block) ->
context = {}
for ns in namespaces
for k, v of ns
if context[k]?
throw "Unable to import namespace: symbol [#{k}] already imported!"
context[k] = v
block(context)
@shawn42
shawn42 / aabb_node.rb
Created January 30, 2012 18:13
2D Axis Aligned Bounding Box Tree Sample
# This is the primary element in an AABBTree.
# It acts as both containing elements and leaf elements. Leaves have an @object,
# containers have @a and @b nodes. All leaf nodes have a cached list of
# collisions that contain references to other nodes in the tree.
class AABBNode
include AABBNodeDebugHelpers
attr_accessor :bb, :a, :b, :parent, :object, :cached_collisions
def initialize(parent, object, bb)
@parent = parent
@shawn42
shawn42 / expander.coffee
Created January 3, 2012 06:29
Presenter First in Backbone.js
namespace "MyWidget.Expander", (exports) ->
template = JST["thingers/widget"]
exports.expand = (model) ->
template({
'$.name': "#{model.get('first_name')} #{model.get('last_name')}"
})
@shawn42
shawn42 / object_space_inspect.rb
Created November 29, 2011 06:48
Performance investigation
puts "\nGARBAGE COLLECTION"
# Not even close to exact, but gives a rough idea of what's being collected
old_objects = ObjectSpace.count_objects.dup
ObjectSpace.garbage_collect
new_objects = ObjectSpace.count_objects
old_objects.each do |k,v|
diff = v - new_objects[k]
puts "#{k} #{diff} diff" if diff != 0
end
@shawn42
shawn42 / pry_cd.rb
Created September 16, 2011 12:44
Pry Examples
pry(main)> cd Document.first/@attributes
pry(#<Hash>):2> keys.sort
=> ["actual_finish_at",
"actual_start_at",
"adjusted_planned_finish_at",
"adjusted_planned_start_at", ...
# HELLO?
class Foo
include Mongoid::Document
field :stats, :type => Hash
end