Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / itinerary.rb
Created November 23, 2010 04:05
shows very simplified version of how I implemented my itinerary
module Itinerary
class Day
def initialize(date,&block)
@date = date
instance_eval(&block)
end
def readings
@readings ||= Array.new
end
@JoshCheek
JoshCheek / extract.rb
Created November 25, 2010 02:44
script to build a web page that shows the links contained on other pages
# script to build a web page that shows the links contained on pages at source urls
# an example solution for the question at http://www.ruby-forum.com/topic/521021
# if these change frequently, it might be better to read them from a file
# rather than store them in the source
urls = [
'http://www.google.com/',
'http://weatherflash.com/usa/ks/wichita/',
'http://umbrellatoday.com/',
def exp_and_msg
return 1 , "hi"
end
exp , msg = exp_and_msg
exp # => 1
msg # => "hi"
@JoshCheek
JoshCheek / fibonacci.doublehelix
Created December 23, 2010 21:25
double helix program to print out fibonacci numbers
#!/usr/bin/env ruby
# probably need to $ gem install doublehelix
require "doublehelix"
GC
A--T
C---G
G----C
A----T
@JoshCheek
JoshCheek / gist:764219
Created January 4, 2011 00:16
sort of based on the monty hall problem
switch = 0
stay = 0
100_000.times do
doors = [ :goat , :goat , :goat , :car ].shuffle
guess = rand doors.size
if doors[guess] == :car
stay += 1
@JoshCheek
JoshCheek / gist:764220
Created January 4, 2011 00:16
monty hall style problem
switch = 0
stay = 0
100_000.times do
doors = [ :goat , :goat , :goat , :car ].shuffle
guess = rand doors.size
if doors[guess] == :car
stay += 1
@JoshCheek
JoshCheek / cons.rb
Created January 9, 2011 13:28
lisp cons cells as lambdas
# While watching Rick DeNatale's talk at RubyConf
# http://confreaks.net/videos/461-rubyconf2010-objects-are-just-objects-aren-t-they
# he shows how SICP introduces cons as lambdas.
# I thought the idea was interesting, so decided to try it out. Done here in Ruby.
def cons head , tail
cons = ->(which) { which == :head ? head : tail }
def cons.inspect(is_head=true)
car = car(self).inspect
cdr = cdr(self).inspect
@JoshCheek
JoshCheek / gist:777727
Created January 13, 2011 11:14
friendly comments on some code posted to the ML
# http://www.ruby-forum.com/topic/860689
def ask question
goodAnswer = false # The Ruby idiom is to use snake_case for variables
while (not goodAnswer) # here, I would say "until goodAnswer"
puts question
response = gets.chomp
attack = rand(11)
attacker = rand(11)
@JoshCheek
JoshCheek / data.yml
Created January 13, 2011 22:51
Showing how I would handle some data by using yaml instead of an ad hoc format.
---
- :category: cat1
:items:
- item1
- item2
- item3
- :category: cat2
:items:
- item1
- :category: cat3
@JoshCheek
JoshCheek / calculator.rb
Created January 18, 2011 01:54
simple sinatra calculator app
# Lets create a simple calculator app together, it should have a get method for localhost:4567/calculator that displays two forms
# the first form should post (note that post is one of those http methods) to localhost:4567/add, and have two fields named "num1" and "num2"
# the second form should post to localhost:4567/subtract with the same args
# the page that they go to should display the result, and have a link to get back to calculator
require 'rubygems'
require 'sinatra'
get '/calculator' do
'