Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
jbrechtel / gist:1113005
Created July 29, 2011 02:19
Git instructions
git init .
git add .
git commit -m "Initial commit"
git remote add origin [email protected]:myproject.git
git push origin master
@jbrechtel
jbrechtel / Classes.scala
Created August 18, 2011 01:23
Scala classes
//simple class
class Person
//inheritance
class Sarah extends Person
//class with constructor arguments
class Jet(wingspan: Int)
//class with constructor argument that maps to public readonly property
@jbrechtel
jbrechtel / classes.rb
Created August 18, 2011 01:43
Ruby classes
#simple class
class Person
end
#inheritance
class Sarah < Person
end
#class with constructor arguments
class Jet
@jbrechtel
jbrechtel / traits.scala
Created August 21, 2011 00:59
Traits in Scala
trait Searchable {
def search(query: SearchQuery): Seq[SearchResults]
}
trait PaginationWithNominalType {
this: Searchable =>
def page(query: SearchQuery, pageSize: Int, pageNum: Int) = {
search(query).grouped(pageSize).toList(pageNum)
}
}
@jbrechtel
jbrechtel / modules.rb
Created August 21, 2011 01:16
Modules in Ruby
module Pagination
def page(params, page_size, page_num)
search(params).in_groups_of(page_size)[page_num]
end
end
@jbrechtel
jbrechtel / collections.scala
Created August 21, 2011 01:48
Scala collections
//creating collections of specific types
val intArray = Array(1,2,3,4)
val intLinkedList = List(4,5,6,7)
val indexedSequenceOfStrings = IndexedSeq("one", "two", "three")
//filtering items in a collection
val evens = List(1,2,3,4,5,6,7).filter(_ % 2 == 0)
//mapping
List("hi", "my", "name", "is", "slim", "shady").map(_.toUpperCase)
@jbrechtel
jbrechtel / gist_tag.rb
Created August 21, 2011 01:49 — forked from BinaryMuse/gist_tag.rb
A Liquid tag for Jekyll sites that allows embedding Gists and showing code for non-JavaScript enabled browsers and readers.
require 'cgi'
require 'digest/md5'
require 'net/https'
require 'uri'
module Jekyll
class GistTag < Liquid::Tag
def initialize(tag_name, text, token)
super
@text = text
@jbrechtel
jbrechtel / collections.rb
Created August 21, 2011 02:01
Ruby collections
#creating a collection.
ints = [1,2,3,4,5,6]
#filtering items in a collection
evens = [1,2,3,4,5,6,7].select { |i| i % 2 == 0 }
#mapping
["hi", "my", "name", "is", "slim", "shady"].map { |s| s.upcase }
#slightly more involved mapping
@jbrechtel
jbrechtel / hashes.rb
Created August 21, 2011 02:36
Ruby hashes
#create a literal hash
cars = {'james' => 'BMW', 'ike' => 'Infiniti', 'stephen' => 'Buick'}
#create a hash from an array
cars = Hash[*['james', 'BMW', 'ike', 'Infiniti', 'stephen', 'Buick']]
#mapping over a hash
#transform all string keys to symbols (interned strings)
Hash[{'name' => 'James', 'job' => 'consultant', 'age' => 28}.flat_map do |(k,v)|
[k.to_sym, v]
@jbrechtel
jbrechtel / maps.scala
Created August 21, 2011 03:02
Scala maps
//create a map
val cars = Map("james" -> "BMW", "ike" -> "Infiniti", "stephen" -> "Buick")
//create a map from an Array
//Scala does not provide a convenience method to do this... :(
val carArray = Array("james", "BMW", "ike", "Infiniti", "stephen", "Buick")
val cars = carArray.grouped(2).toList.map(c => (c.head,c.last)).toMap
//mapping over a map
//transform all string keys to symbols (interned strings)