Skip to content

Instantly share code, notes, and snippets.

# stolen from rosetta code to look at later
def eratosthenes(n)
nums = [nil, nil, *2..n]
(2..Math.sqrt(n)).each do |i|
(i**2..n).step(i){|m| nums[m] = nil} if nums[i]
end
nums.compact
end
p eratosthenes(100)
@vapidbabble
vapidbabble / grade_parser.rb
Last active May 20, 2016 14:20
haskell example in ruby
#first stab
h=Hash.new
(File.read "grades.txt").split("\n").each do |pair|
(person, grade)=pair.split ' '
if h[person]
h[person] << grade.to_i
else
h[person]=[grade.to_i]
end
end
@vapidbabble
vapidbabble / fizzbuzz.clj
Last active September 6, 2015 21:33 — forked from walkermatt/fizzbuzz.clj
fizzbuzz without conditionals in Clojure
;; fizzbuzz without conditionals in Clojure
; Simple patten matching using a single map lookup
(defn fizzbuzz [x]
(let [v [(= (mod x 3) 0) (= (mod x 5) 0)]]
({[true false] "fizz"
[false true] "buzz"
[true true] "fizzbuzz"
[false false] x} v)))
@vapidbabble
vapidbabble / gist:e7f7e9407e6fe5a632c2
Last active August 29, 2015 14:24
Unicursal hexagram in Elm
import Color exposing (..)
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
hexagon : Color -> Form
hexagon clr = outlined (solid clr) (ngon 6 150)
hex_on_a_point : Float -> Color -> Form
hex_on_a_point deg clr = rotate (degrees deg) (hexagon clr)
@vapidbabble
vapidbabble / need_test_for.txt
Created September 17, 2013 03:14
This is a simple problem for which I need help writing tests in Ruby (minitest Rspec whatever). I've written the code already in another language. It just seems when I'm writing tests for 'how' something should work I keep skipping to the making it work part.
Loop through a given directory's subdirectories ( 1 deep)
In each subdirectory check only files that are older than a day.
If the file is a PDF file or zero byte move to an archive directory and log.
At the end, mail the list of files, if any, to somebody.

Hi,

I would love to hear your opinion on the idea of “combining RDoc and RSpec to create testable documentation”. Let me explain:

Given you have several RDoc files like this one:

= Examples

Some example code snippets.
.DS_Store
*.csv
*.pdf
h scroll left
j scroll down
k scroll up
l scroll right
gg scroll to top of the page
G scroll to bottom of the page
f activate link hints mode to open in current tab
F activate link hints mode to open in new tab
r reload
# Newbie Programmer
def factorial(x)
if x == 0
return 1
else
return x * factorial(x - 1)
end
end
puts factorial(6)
puts factorial(0)
require 'rss/2.0'
require 'open-uri'
module ApplicationHelper
class EmptyURL < RuntimeError
end
def rss_content(name)
content_object = StaticContent.find_by_name(name)
if content_object.nil? || (Time.now - content_object.updated_at) > 4.hours