Skip to content

Instantly share code, notes, and snippets.

ruby-1.9.2-p180 :001 > cat = Object.new
=> #<Object:0x0000010085bc58>
ruby-1.9.2-p180 :002 > puts cat.methods.sort
!
!=
!~
<=>
==
===
=~
ruby-1.9.2-p180 :004 > def cat.has_nine_lives?
ruby-1.9.2-p180 :005?> true
ruby-1.9.2-p180 :006?> end
=> nil
ruby-1.9.2-p180 :007 > cat.has_nine_lives?
=> true
@gnarmis
gnarmis / gist:1050563
Created June 28, 2011 05:43
Finding the Largest Prime Factor of a Composite Number
def maxPrimeFactor(n) #=> maxPrimeFactor(600851475143) => 6857 in 2007.562 ms
# limit the ridiculous range safely
range = n**(1/Math.sqrt(n.to_s.length).floor.to_f)
ary = (1..range).to_a
# Sieve of Eratosthenes (replace with Sieve of Atkin?)
for i in ary
for j in ary
if i != j
if (j % i == 0)
ary.delete(j)
@gnarmis
gnarmis / sassycoffee.sh
Created August 9, 2011 21:23
Single script to watch and compile both CoffeeScript and Sass files
#!/bin/bash
# run `compass watch` at pwd
# run `coffee -o scripts/ -cw coffee/` at pwd
type -P compass &>/dev/null || { echo "Compass command not found."; exit 1; }
type -P coffee &>/dev/null || { echo "Coffee command not found."; exit 1; }
if [ ! -d sass/ ] || [ ! -d scripts/ ]
then
echo "Project not setup correctly! Put sass files in sass/ and coffee in coffee/"
else
@gnarmis
gnarmis / piping_example.rb
Created September 19, 2012 21:09
Piping arguments through multiple functions in Ruby
# piping example in Ruby
def foo(data)
data[:a] += 1
data
end
def bar(data)
data[:b] += 10
data
@gnarmis
gnarmis / experiments.rb
Created September 21, 2012 09:13
Experimenting with bastardized functional-ness in Ruby
#ruby1.9.3
def defn name, &b
Object.send :define_method, name, &b
end
# this also works (surprisingly), albeit with a warning
# def defn name
# Object.send(:define_method, name)
# end
@gnarmis
gnarmis / functional_hash.rb
Created September 26, 2012 16:57
Ruby Hashes as functions of keys to values
# In Clojure, hash-maps are truly functions of keys to values.
# So you can do `(:a {:a 1})` and get `1` as the result
# Why not put this in Ruby?
# access keys of a hash like a function
class Object
def respond_to?(method)
if (method.to_s =~ /^_.*/) == 0
true
@gnarmis
gnarmis / example_user.json
Created December 7, 2012 03:19
Example Twitter User Features
"user": {
"contributors_enabled": false,
"created_at": "Wed Sep 23 18:48:46 +0000 2009",
"default_profile": false,
"default_profile_image": false,
"description": "Mura is a Japanese Fusion Restaurant serving steak, sushi, seafood. Located in North Hills. Serving lunch and dinner.",
"favourites_count": 0,
"follow_request_sent": null,
"followers_count": 665,
"following": null,
@gnarmis
gnarmis / gardner.hs
Last active December 10, 2015 06:28
Gardner's canceling digits puzzle, also related to Project Euler problem #33
-- turn an Integer into a list of digits
digits = map (read . (:[])) . show
-- is a given fraction special?
special :: Integer -> Integer -> Bool
special n d
| (n_rf / d_rf) == ((realToFrac (head ns)) / (realToFrac (head ds))) = True
| otherwise = False
where n_rf = realToFrac n
d_rf = realToFrac d
@gnarmis
gnarmis / euler-three.clj
Created January 5, 2013 18:47
Solution to Project Euler problem 3. Require Clojure 1.5 RC1 and runs in less than a quarter of a second in my Clojure REPL.
(ns euler.three
(require [clojure.core.reducers :as r]))
(declare largest-prime-factor-for)
(declare factors-of)
(declare source-factors)
(declare source-naturals)
(declare factor?)
(declare prime?)
(declare certainty)