Skip to content

Instantly share code, notes, and snippets.

@bschaeffer
bschaeffer / args.rb
Created March 7, 2012 23:28
Ruby *args examples
ids = [1,2,3]
def targs(*ids)
puts ids.length
puts ids.flatten.length
end
targs ids
#=> 1
#=> 3
@Fitzsimmons
Fitzsimmons / spark.md
Created April 25, 2012 15:00 — forked from jesperfj/spark.md
Spark on Heroku

This guide will get you started using Spark on Heroku/Cedar. Spark is basically a clone of Sinatra for Java. 'Nuff said.

Create your app

Create a single Java main class in src/main/java/HelloWorld.java:

import static spark.Spark.*;
import spark.*;
def destructure(method_name)
# Intercept the original class
meta_klass = class << self; self end
# Save the original method as a proc
method_proc = method(method_name)
# We only want to do this for keyword argument type
# methods
unless method_proc.parameters.all? { |t, _| t == :key }
@lilianchisca
lilianchisca / picturefill.html
Last active March 6, 2019 15:14
Lazy load reesponsive webp images with jpeg fallback using Modernizr.on // based on https://github.com/scottjehl/picturefill
<div itemprop="image" data-picture="lazy" data-alt="Don't forget to change the alt text!">
<div data-src="assets/images/example-s"></div>
<div data-src="assets/images/example-m" data-media="(min-width: 700px)"></div>
<div data-src="assets/images/example-l" data-media="(min-width: 1200px)"></div>
<noscript>
<img src="assets/images/example-s.jpg" alt="Don't forget to change the alt text!">
</noscript>
</div>
@Integralist
Integralist / Hash Keys to Symbols.rb
Last active September 2, 2020 08:50
Convert Ruby Hash keys into symbols
hash = { 'foo' => 'bar' }
# Version 1
hash = Hash[hash.map { |k, v| [k.to_sym, v] }]
# Version 2
hash = hash.reduce({}) do |memo, (k, v)|
memo.tap { |m| m[k.to_sym] = v }
end
@barneycarroll
barneycarroll / animator.js
Last active June 11, 2021 05:06
A factory for decorating Mithril modules / views / elements with incoming and outgoing animations.
var animating = false;
// Define an animator consisting of optional incoming and outgoing animations.
// alwaysAnimate is false unless specified as true: false means an incoming animation will only trigger if an outgoing animation is also in progress.
// forcing dontClone to true means the outward animation will use the original element rather than a clone. This could improve performance by recycling elements, but can lead to trouble: clones have the advantage of being stripped of all event listeners.
function animator( incoming, outgoing, alwaysAnimate, dontClone ){
// The resulting animator can be applied to any number of components
return function animate( x, y, z ){
var config;
var parent;
@solnic
solnic / type_safe_entity_vs_ar.rb
Last active November 24, 2021 02:23
Fetching type-safe entity with rom repo vs AR
Calculating -------------------------------------
type-safe users 273.000 i/100ms
ar user models 257.000 i/100ms
-------------------------------------------------
type-safe users 2.813k (± 1.7%) i/s - 14.196k
ar user models 2.574k (±10.7%) i/s - 12.850k
Comparison:
type-safe users: 2812.7 i/s
ar user models: 2574.2 i/s - 1.09x slower
@jesperfj
jesperfj / spark.md
Created September 16, 2011 18:19
Spark on Heroku

This guide will get you started using Spark on Heroku/Cedar. Spark is basically a clone of Sinatra for Java. 'Nuff said.

Create your app

Create a single Java main class in src/main/java/HelloWorld.java:

:::java
import static spark.Spark.*;
import spark.*;
@justinko
justinko / Plea.markdown
Created May 30, 2012 19:40
Am I doing it wrong?

Dear Rubyists,

I just lost a contract because of my code in a Rails project.

The specific code in question is related to a "posting a comment" feature. Here are the details:

In this project, "posting a comment" does not simply entail inserting a row into the database. It involves a procedure to yes, insert a row, but also detect its language, check for spam, send emails, and "share" it to Twitter and Facebook. I believe this algorithm should be encapsulated. I do not believe it belongs in a controller or a model. I do not believe Active Record callbacks should be used.

The "senior developer", whom is the stake holder's right hand man, said this:

@xsot
xsot / instructions.md
Last active March 3, 2024 13:42
sed maze solver

Usage

sed -E -f solver.sed input where input is a file containing the maze.

For best results, resize your terminal to match the height of the maze. To disable animations, delete the lines containing p.

Maze format

The solver assumes the following:

  • The maze only contains the characters # \nSE
  • Every line has the same number of characters
  • There is only one start (S) and end (E)