Dynamic Rails 3 Queries

Have methods and arguments for a query but want to run it in the background? Do something like this

Scala Idioms

Scala Idioms: quirky as hell, super terse, awesome

Console multi-line input

   Iterator.continually(Console.readLine).takeWhile(_ != "").foreach(_.split(" ").map(_.toInt))


 import scala.collection.mutable.ListBuffer;  
 var q = ListBuffer[List[Int]]();  
 5.until(10) foreach(i => q += (0 to i).toList)  

  • ArrayBuffer is the best-performing solution as long as you only need to append to one end
  • It's usually faster to append to an ArrayBuffer than to prepend to a List (or add an element to a ListBuffer)
    • Lists require the creation of a wrapper object
    • ArrayBuffer only needs to copy the object (generally twice): copies are usually faster than object creation

Seq (immutable)

 val q: Seq[List[Int]] = 5.until(10).map(i => (0 to i).toList).toSeq  
 => q: Seq[List[Int]] = Vector(List(0, 1, 2, 3, 4, 5), List(0, 1, 2, 3, 4, 5, 6), List(0, 1, 2, 3, 4, 5, 6, 7), List(0, 1, 2, 3, 4, 5, 6, 7, 8), List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))  


 val dims = Seq(3,4,5)  
 def dim(line: String): Either[Boolean,Int] = {  
  try { Right(line.toInt) }  
  catch { case e => Left(false) }  
 dims.foreach { d =>   
  dims(d) match {  
   case Right(i: Int) => println(i)  
   case Left(b: Boolean) => println("fail")  

Tail recursion

 import scala.annotation.tailrec  
 class Factorial2 {  
  def factorial(n: Int): Int = {  
   @tailrec def factorialAcc(acc: Int, n: Int): Int = {  
    if (n <= 1) acc  
    else factorialAcc(n * acc, n - 1)  
   factorialAcc(1, n)  
 object factBot extends Factorial2 {  
  def compute(i: Int): Int = {  
   lazy val res:Int = factorial(i).toInt  
   return res  


 val words = Seq("hello", "dog")  
 ("" /: words) (_ +" "+ _)  
 import scalaz._  
 import Scalaz._  
 (∅[String] /: words) (_ +" "+ _)  


  • Scala: Seq ~ Java: List
  • Scala: List ~ Java: LinkedList
  • List has O(1) prepend and head/tail access.
  • Most other List operations are O(n)
  • A "Scala Array" is a Java Array
  • Array is the only parameterized data structure on JVM which does not suffer from boxing, which provides it superior performance in many ways.
  • IndexedSeq, on the other hand, is a trait in Scala extended by collections which provide effective constant time indexed access to their elements
    • position of elements (indices) deterministically defined.
    • There are generic, mutable and immutable versions of this trait, and an implicit conversion from Array to it.

Scala 2.10 + Play 2.1 + Pusher

Scala, play, and pusher, oh my

Adventures in Play 2.0 with Scala, Salat, and Casbah

How to get play-salat and play-cashbah working in the Play 2.0 Scala web framework

Scala? It's object-oriented. It's functional. It's complex. It's fun.
Scala has anonymous functions, type inference, list comprehensions, lazy initialization, excellent pattern matching, case classes, delimited continuations, higher-order types, and complexity that exceeds 10.

What is that stuff after the mapping binder? The apply and unapply functions of the Post model are curried with the form mapping. Also check out the views.html.helper package in the Play 2.0 API.

Controller: controllers/Posts.scala
View: views/posts.scala.html

Go introspection


rails-debate github repo

I just made my rails debates site open-source and pushed it to a github repository.

What is rails-debate? What if I hate arguing and avoid conflict?

User-driven debate site built with Ruby on Rails and jQuery. Users can create and reply to arguments and join sides. Easily deployed on Heroku after resolving gem dependencies.

Required gems

  • 'rails', '3.0.3' 
  • 'clearance', '0.9.1' 
  • "will_paginate", "~> 3.0.pre2" 
  • 'acts-as-taggable-on' 

Programmatically setting images as thumbnails in Magento

Magento is evil. The PHP file structure is insane and confusing. Do I want to manually set 500 products' photos to thumbnails? Fuck no. Here's how to programmatically set uploaded images as a product's thumbnail, small image, and large image.