Skip to content

Instantly share code, notes, and snippets.

@mhriess
mhriess / simple_recursion_examples.scala
Last active August 29, 2015 14:05
simple recursion examples
def sumInts(a: Int, b: Int): Int =
if (a > b) 0 else a + sumInts(a + 1, b)
def countChange(money: Int, couns: List[Int]): Int = {
if (money == 0) 1
else if (money < 0) 0
else if (coins.isEmpty && money >= 1) 0
else {
countChange(money, coins.tail) + countChange(money - coins.head, coins)
}

Using jQuery

jQuery has become the most popular JavaScript library both inside and outside the Rails community. Let's look at how to take advantage of the library with our applications.

Setup

The setup process differs depending on whether your app is running on a version of Rails before 3.1 or greater than 3.1.

Before Rails 3.1: jquery-rails

@mhriess
mhriess / friendly_urls.markdown
Created November 13, 2012 19:17 — forked from agnellvj/friendly_urls.markdown
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@mhriess
mhriess / card.rb
Created October 25, 2012 04:38 — forked from dbc-challenges/card.rb
FlashCardinator
class Session
attr_reader :output_stream
def initialize(game, input_stream = $stdin, output_stream = $stdout)
@game = game
@input_stream = input_stream
@output_stream = output_stream
end
require 'simplecov'
SimpleCov.start
require './binary_search.rb'
describe "#binary_search" do
let(:array_1) { [1,2,3,4,5,6,7] }
let(:array_2) { ["apple", "pear", "banana", "pineapple", "kiwi"].sort}
it "is defined as a method" do
@mhriess
mhriess / card.rb
Created October 18, 2012 19:11 — forked from dbc-challenges/card.rb
FlashCardinator
require 'csv'
require 'term/ansicolor'
# Use this trick to work around namespace cluttering that
# happens if you just include Term::ANSIColor:
class Color
extend Term::ANSIColor
end
@mhriess
mhriess / RPS_bad.rb
Created October 5, 2012 05:48
Rock-Paper-Scissors bad
CHOICES = ["Rock", "Paper", "Scissors"]
def new_game
print %q"Let's play some Rock-Paper-Scissors!
Enter your choice below:
>"
outcome
end
@mhriess
mhriess / rock_paper_scissors.rb
Created October 5, 2012 05:47
Rock-Paper-Scissors
CHOICES = ["Rock", "Paper", "Scissors"]
def player_choice
print "Enter your choice below:
>"
player_input = gets.chomp.capitalize
CHOICES.include?(player_input) ? player_input : player_choice
end
@mhriess
mhriess / RPS_pseudocode.md
Created October 5, 2012 05:46
Rock-Paper-Scissors Pseudocode

Script: ROCK - PAPER - SCISSOR Iteration One: COMPUTERS HANDSIGN

RETURN a random hand sign (Rock-Paper-Scissor)

Iteration Two: CHECK WHO WON

IF COMPUTERS HANDSIGN beats PLAYERS HANDSIGN RETURN "You loose" ELSIF PLAYERS HANDSIGN beats COMPUTERS HANDSIGN RETURN "You win" ELSE RETURN "Draw" ENDIF

Iteration Three: GET A HANDSIGN OF A PLAYER

# Put your answers here!
Michael-Riess-Computer:sample_project michaelriess$ rvm list
rvm rubies
ruby-1.8.7-p302 [ i686 ]
ruby-1.9.2-p290 [ x86_64 ]
* ruby-1.9.2-p318 [ x86_64 ]
ruby-1.9.2-p320 [ x86_64 ]