Skip to content

Instantly share code, notes, and snippets.

View bkempner's full-sized avatar

Ben Kempner bkempner

  • Auto List
  • Austin, TX
View GitHub Profile
SELECT lat, lon, SQRT( POW( 69.1 * ( lat - 33.673989 ) , 2 ) + POW( 69.1 * ( 111.858430 - lon ) * COS( lat / 57.3 ) , 2 ) ) AS distance FROM cities where distance < 25 ORDER BY distance;
def test_class(o)
case o.class
when Foo
puts "foo class"
else
puts "not found"
end
end
o = Foo.new
@bkempner
bkempner / gist:1093545
Created July 19, 2011 19:53
Rspec wtf?
## Controller
class FooController < ActionController::Base
def wtf
SomeObject.foo
end
end
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 >
ruby-1.9.2-p180 :002 >
ruby-1.9.2-p180 :003 >
ruby-1.9.2-p180 :004 >
ruby-1.9.2-p180 :005 > ObjectSpace.each_object(Class).select { |v| v < Snippet }
=> []
ruby-1.9.2-p180 :006 > Snippet::Cheap.new => #<Snippet::Cheap id: nil, location_id: nil, listing_id: nil, type: "Snippet::Cheap", content: nil, approved: false>
ruby-1.9.2-p180 :007 > ruby-1.9.2-p180 :008 > ObjectSpace.each_object(Class).select { |v| v < Snippet }
=> [Snippet::Cheap(id: integer, location_id: integer, listing_id: integer, type: string, content: text, approved: boolean)]
Loading development environment (Rails 3.0.7)
ruby-1.9.2-p180 :001 >
ruby-1.9.2-p180 :002 >
ruby-1.9.2-p180 :003 >
ruby-1.9.2-p180 :004 > Snippet::Cheap < Snippet
=> true
ruby-1.9.2-p180 :005 > ObjectSpace.each_object(Class).select { |v| v < Snippet }
=> [Snippet::Cheap(id: integer, location_id: integer, listing_id: integer, type: string, content: text, approved: boolean)]
ruby-1.9.2-p180 :006 >
## spec
it '#index' do
mock(controller).index
get :index
response.should be_success
end
## controller
class_eval traced_method, __FILE__, __LINE__
require 'spec_helper'
describe 'wtf' do
it "doesn't allow non-mocked method calls if any methods are mocked" do
u = User.new
mock(u).first_name
u.first_name
u.last_name
end
require 'spec_helper'
describe 'wtf' do
it "doesn't allow calls to a mocked method with different args" do
mock(User).find
User.find(1)
User.find(23) # where uses find internally
end
# error unexpected find(23) called
foo = case
when true
'foo'
else
'bar'
end
# foo = 'foo'
foo = case
when true