Skip to content

Instantly share code, notes, and snippets.

View kaiwren's full-sized avatar

Sidu Ponnappa kaiwren

View GitHub Profile
@kaiwren
kaiwren / any_instance_mocha.rb
Created March 25, 2011 13:50
any_instance behaves like at_least_one_instance for expectations and like every_instance for stubs in mocha
require "spec_helper"
describe "any_instance in mocha" do
it "should bar" do
Array.any_instance.stubs(:foo).returns(1)
[].foo.should eq(1) # Every instance of Array seems to be
[].foo.should eq(1) # stubbed (as one would expect)
end
it "should foo" do
@kaiwren
kaiwren / exception_redefine_bactrace.rb
Created March 23, 2011 20:26
Redefining backtrace on Exception causes message to break on JRuby 1.6.0
e = Exception.new
puts e.message # "Exception"
def e.backtrace
[]
end
puts e.message # "Exception" on 1.8.6/1.8.7/1.9.2, #<Class:0x11dff2e1b> on JRuby 1.6.0
@kaiwren
kaiwren / file_read_fail.rb
Created March 23, 2011 00:39
This issue has the build breaking on rspec-core on Jruby 1.6.0
# From https://github.com/rspec/rspec-core/issues/195
# and patched (unsuccessfully on JRuby) in https://github.com/rspec/rspec-core/issues/295
String.send :alias_method, :to_int, :to_i
puts File.readlines(__FILE__).empty? # is false on 1.8 and 1.9, true on JRuby 1.6.0
Seems that I have been held, in some dreaming state
A tourist in the waking world, never quite awake
No kiss, no gentle word could wake me from this slumber
Until I realise that it was you who held me under
Felt it in my fist, in my feet, in the hollows of my eyelids
Shaking through my skull, through my spine and down through my ribs
No more dreaming of the dead as if death itself was undone
No more calling like a crow for a boy, for a body in the garden
@kaiwren
kaiwren / hash_ordering_18.rb
Created February 23, 2011 18:31
Hash ordering in Jruby vs CRuby
~ λ rvm jruby-head
~ λ ruby -v
jruby 1.6.0.dev (ruby 1.8.7 patchlevel 249) (2011-01-13 b9f8624) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
~ λ irb
jruby-head :001 > {:a => :b, :c => :d}.select{true}
=> [[:a, :b], [:c, :d]]
jruby-head :002 > {:a => :b, :c => :d}.select{true}
=> [[:a, :b], [:c, :d]]
jruby-head :003 > {:a => :b, :c => :d}.select{true}
=> [[:a, :b], [:c, :d]]
@kaiwren
kaiwren / js_route.js
Created January 31, 2011 15:50
js routes from ruby routes
FooNamespace.Routes = {
myResourcePath: function(){ return '<%= my_resource_path(:format => 'json') %>' }
}
@kaiwren
kaiwren / quote.txt
Created October 30, 2010 19:16
Managers, management, churches and religion.
The first myth of management is that it exists. Managers are to management as churches are to religion.
ruby-1.8.7-p174 > u = URI.parse('http://foo:bar@localhost:3000')
=> #<URI::HTTP:0x101e2bc40 URL:http://foo:bar@localhost:3000>
ruby-1.8.7-p174 > u.user=nil
=> nil
ruby-1.8.7-p174 > u
=> #<URI::HTTP:0x101e2bc40 URL:http://localhost:3000>
ruby-1.8.7-p174 > u.to_s
=> "http://localhost:3000"
@kaiwren
kaiwren / write_to_clipboard.rb
Created October 3, 2010 21:20
Write any string to the clipboard in OSX
IO.popen('pbcopy', "w").tap{|io| io.write "some string"}.close
@kaiwren
kaiwren / net_http_debug.rb
Created September 22, 2010 12:48
Enable debug for all Ruby HTTP requests
require 'net/http'
module Net
class HTTP
def self.enable_debug!
raise "You don't want to do this in anything but development mode!" unless Rails.env == 'development'
class << self
alias_method :__new__, :new
def new(*args, &blk)
instance = __new__(*args, &blk)
instance.set_debug_output($stderr)