Skip to content

Instantly share code, notes, and snippets.

@joecorcoran
joecorcoran / block_exercises.rb
Last active August 29, 2015 14:13
Working with yield in Ruby
# Define a method named add_one that accepts
# a block and returns the return value of the
# block + 1, as follows.
# (Hint: yield gives you the return value of
# the block)
add_one do
2
end
# => 3
@joecorcoran
joecorcoran / It's require time!
Created July 23, 2013 09:15
Thanks that's great.
$ irb
1.9.3-p374-perf :001 > Time
=> Time
1.9.3-p374-perf :002 > Time.now
=> 2013-07-23 10:04:42 +0100
1.9.3-p374-perf :003 > Time.now.iso8601
NoMethodError: undefined method `iso8601' for 2013-07-23 10:04:47 +0100:Time
from (irb):3
from /Users/joecorcoran/.rvm/rubies/ruby-1.9.3-p374-perf/bin/irb:16:in `<main>'
1.9.3-p374-perf :004 > require 'time'
@joecorcoran
joecorcoran / gist:3818133
Created October 2, 2012 10:48
Jasmine toBeNear matcher
beforeEach(function() {
this.addMatchers({
toBeNear: function(expected, within) {
within = within || 1;
return (this.actual >= (expected - within)) && (this.actual <= (expected + within));
}
});
});
@joecorcoran
joecorcoran / benchmark.txt
Created September 29, 2011 07:22
cities gem: YAML vs JSON
10.times { City.cities_in_country("RU") }
Benchmark.bm
------------
user system total real
JSON 33.210000 0.630000 33.840000 ( 33.872267)
YAML 78.530000 3.230000 81.760000 ( 81.758585)
Benchmark.bmbm
--------------
toHaveEvent: function(eventType){
return (this.actual.data('events')) && (typeof this.actual.data('events')[eventType] == 'object');
}
Name Department Manager Salary
Robin Hood 200
Arsene Wenger Bar Friar Tuck 50
Friar Tuck Foo Robin Hood 100
Little John Foo Robin Hood 100
Sam Allardyce 250
Dimi Berbatov Foo Little John 50
def smallest_temperature_spread
# store all munged values in a hash
map = {}
File.open("weather.dat", "r") do |file|
# iterate each line
while line = file.gets
# only use relevant lines
if line.match(/^\s+\d+\s+[\d\*]+\s+/)
# seems appropriate to convert keys to integers here for possible future use
function DoTooMuchStuff(num) {
$('p#mypar_'+num).slideUp(400).remove();
$('h3#myheading_'+num).html('No records found');
context.trigger('updateStoredRecords', { remove: num });
var delimiter = ($('ul#removed > li').size() > 0) ? ', ' : '';
$('ul#removed').append($('<li/>').html(delimiter+GetName(num)));
};
var app = $.sammy(function() {
var context = this;
this.get('#/', function() {
this.trigger('myEvent', { h1: 'My app', text: 'world' });
});
this.bind('myEvent', function(e, opts) {
$('h1').html(opts.h1);
javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){su=$("link[rev=canonical]").attr("href");su===undefined?alert("No%20short%20url!%20Try%20viewing%20the%20image%20directly,%20outside%20of%20any%20set."):alert(su);});