Skip to content

Instantly share code, notes, and snippets.

@cupakromer
cupakromer / gist:2773353
Created May 23, 2012 04:57
which way is the way?
# The points are stored in a nested hash
# {
# "bounds"=> {
# "northeast"=>{"lat"=>39.0464399, "lng"=>-76.9836289},
# "southwest"=>{"lat"=>38.995162, "lng"=>-77.033157}
# },
# }
# We just want the values, as an array:
# [north_lat, east_lng, south_lat, west_lng]
def extract_bounding_box_version1
module Fetcher
class Base
# ... other code ...
def uri
@cue
end
def options
{}
module Fetcher
class BasicTraffic < Base
add_fetcher_options after: :count_severe_incidents
def self.inherited( subclass )
subclass.add_fetcher_options after: :count_severe_incidents
super
end
private
@cupakromer
cupakromer / .rspec
Created June 10, 2012 13:16 — forked from coreyhaines/.rspec
Loading just active record
--colour
-I app
require "money"
class Decorator < BasicObject
undef_method :==
def initialize(component)
@component = component
end
def method_missing(name, *args, &block)
@cupakromer
cupakromer / basic.rb
Created August 4, 2012 13:12
Steel City Ruby Lightning Talk
def steelcityruby
"I heard you like programming."
end
@cupakromer
cupakromer / gist:3371003
Created August 16, 2012 15:23
each_with_object vs inject
my_array = %w[test one two three]
# Inject takes the value of the block and passes it along
# This often causes un-intended errors
my_array.inject({}) do |transformed, word|
puts transformed
transformed[word] = word.capitalize
end
# Output:
# {}
@cupakromer
cupakromer / Guardfile
Created September 8, 2012 21:17
Guard double test run
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
end
@cupakromer
cupakromer / abstract.md
Created October 3, 2012 03:40
Abusing Class and Struct

In tonight's meeting I tried, and failed, to show how to create a Struct with a custom method. Turns out, you can't do that. But you can use Class.new to accomplish this. UPDATE: You can do this. I don't remember what syntax I was trying before, but in 1.9.3 you can successfully pass a block to Struct.new; though this isn't documented.

The end result is the same:

  • A new Class object
  • Default initialize handling the attributes
  • attr_accessor methods defined for those attributes

Big thanks to @steveklabnik for his two posts which inspired me to try to show this horribleness (that's a reflection on my silliness, not Steve):

@cupakromer
cupakromer / gist:3867596
Created October 10, 2012 18:42
Is there a better way to have this?

So recently I was looking for a way to do a comparison on a String with either another String or a Regexp. Looking online didn't yield much help, most discussions on equality focused on ==, eql?, equal?. None of which would satisfy the requirement. So I was left with this code:

def matches(compare_with)
  if compare_with.is_a?(Regexp)
    @data_string =~ compare_with
 else