Skip to content

Instantly share code, notes, and snippets.

@ctford
ctford / week05.rb
Last active December 14, 2015 23:58
Week 5's Ruby Friday code! The main points from this week are, using methods on Arrays and starting to write test functions so that you know your code works.
# A function to make a person Hash.
def make_person(first_name, second_name, age)
person =
{"first_name" => first_name,
"last_name" => second_name,
"age" => age}
return person
end
# Returns true if person1 is older than person2, otherwise false.
@ctford
ctford / homework_week06.rb
Created April 19, 2013 16:52
Homework for week 06, concentrating on practicing testing and working with built-in methods on Arrays.
# Write a function that returns true if youngest_person() works correctly.
# *Then*, write youngest_person() and check that it's correct using your test.
# youngest_person() should take an Array of people and return the youngest one.
#
# Hint: Consider using max or min, which are defined on the ruby docs site.
# http://ruby-doc.org/core-1.9.3/Array.html
def youngest_person(people)
# ??? <-- Only do this after writing the test and making sure it starts out failing!
end
@ctford
ctford / week07.rb
Last active December 16, 2015 16:49
Week 7's Ruby Friday!
# Week 07
# Homework from last week...
#
# Write a function that returns true if youngest_person() works correctly.
# *Then*, write youngest_person() and check that it's correct using your test.
# youngest_person() should take an Array of people and return the youngest one.
#
# Hint: Consider using max or min, which are defined on the ruby docs site.
# http://ruby-doc.org/core-1.9.3/Array.html
@ctford
ctford / wc-core.clj
Last active December 16, 2015 16:59
Simple word-count command-line and web application.
(ns wc.core
(:gen-class)
(:use compojure.core)
(:require [compojure.route :as route] ; Remember to import compojure "1.1.5"
[ring.middleware.params :as parameters] ; in your project.clj
[clojure.string :as string]))
; \space is a space character
(def is-space?
(fn [character]
@ctford
ctford / comp.clj
Created May 3, 2013 07:05
2/5/2013, Vim and function composition
; Last time we covered two main things. Using Vim to bring the REPL inside your editor,
; and various ways to compose functions.
; We'd already passed functions in as arguments to other functions...
(map inc [1 2 3])
(filter even? [1 2 3])
(reduce + [1 2 3])
; We then saw an example of "wrapping" a function to form a new one.
(def safe-inc (fnil inc 0))
@ctford
ctford / week08.rb
Last active December 16, 2015 22:39
Week 8 - in which we discover classes!
# Week 08
# Last week we built our own assert_equals to help our tests.
def assert_equals(expected, actual, failure_message)
if expected == actual
"Ok!"
else
"Expected '" + expected.to_s + "', but found '" + actual.to_s + "': " + failure_message
end
end
# Week 09
# Last week we built our first class.
#
# Remember, in Ruby, we usually group data and functions together into
# things we call "objects".
#
# An object, is a specific example of a "class". For example, Fred might
# be an object from the class Person.
@ctford
ctford / week10.rb
Last active December 17, 2015 10:59
Week 10 of Ruby Fridays!
# Last week we worked with our own classes.
#
# Remember, in Ruby, we usually group data and functions together into
# things we call "objects".
#
# An object, is a specific example of a "class". For example, Fred might
# be an object from the class Person.
#
# A group of people might be an object from the class Family.
#
@ctford
ctford / Family.rb
Created May 17, 2013 14:08
Family class for week 10.
class Family
def initialize(persons)
@people = persons
end
def youngest()
@people.min do |person1, person2|
person1.age - person2.age
end
end
@ctford
ctford / Person.rb
Created May 17, 2013 14:10
Person class for week 10.
class Person
def initialize(first_name, last_name, age)
@first_name = first_name
@last_name = last_name
@age = age
end
def name()
@first_name + " " + @last_name
end