Skip to content

Instantly share code, notes, and snippets.

@romaimperator
romaimperator / middleware.rb
Created March 4, 2018 03:59
Middleware to support prefixed value in the url
class MyMiddleware
def initialize(app)
@app = app
@message = message
end
def call(env)
dup._call(env)
end
@romaimperator
romaimperator / complex_yield_and_enumerator_example.rb
Created January 26, 2016 03:27
This is a fairly complicated example showing off how cool Ruby's yield and enumerators.
# Below is a complex example of using yielding methods to make a nice interface to use. As you
# can see in RankList#fill!, we don't need to care or check if the location really is empty.
# We also don't need to do the calculation to find the next location to insert at. We can
# iterate through each available location until we reach the end. A cool side effect is that
# we can use any of the enumerable methods on the returned enumerator from #fill_locations
# such as printing the positions of all of the empty spaces by doing:
# p alignment_strategy.fill_locations(@the_grid).to_a
module AlignmentStrategy
class Left
module VirtualDate
extend ActiveSupport::Concern
class_methods do
def virtual_date(name)
attr_reader name
# Incoming forms pass dates as a hash like {1 => 2014, 2 => 4, 3 => 30}
# This converts that to a date class instance
define_method("#{name}=") do |new_value|
def mapper(date)
if date.last_day_of_month?
date..31
else
date
end
end
class Location < ActiveRecord::Base
has_many :bids
def accepted_bids
bids.merge(Bid.accepted)
end
end
class Bid < ActiveRecord::Base
belongs_to :location
next_frame_button.addEventListener(MouseEvent.CLICK, go_to_next_frame);
function go_to_next_frame(event:MouseEvent):void
{
nextFrame();
}
import flash.events.Event;
import flash.net.URLLoader;
var XML_URL:String = "http://yourdomainwithxml/";
var myLoader:URLLoader;
var myXML:XML = new XML();
function xmlLoaded(event:Event):void
{
myXML = XML(myLoader.data);
<specializations>
<specialization>
<type>being awesome</type>
<doctors>
<doctor>
<name>Dan Fox</name>
</doctor>
<doctor>
<name>Nikki Rodriguez</name>
</doctor>
# Returns true if the "X-Requested-With" header contains "XMLHttpRequest"
# (case-insensitive). All major JavaScript libraries send this header with
# every Ajax request.
def xml_http_request?
@env['HTTP_X_REQUESTED_WITH'] =~ /XMLHttpRequest/i
end
alias :xhr? :xml_http_request?
Array.prototype.unique = function(a){
return function(){
return this.filter(a)
}
}(function(a,b,c){
return c.indexOf(a,b+1)<0
});
Array.prototype.unique = function() {
return this.filter(function(a,b,c) {