Skip to content

Instantly share code, notes, and snippets.

@nickserv
nickserv / ubuntu_boot_repair.sh
Created August 10, 2012 05:47
Install and run "Boot Repair" tool on Ubuntu (works on live CDs)
sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install -y boot-repair
boot-repair
@nickserv
nickserv / quotes.rb
Last active October 13, 2015 01:18
Quotes Script
#!/usr/bin/env ruby
# quotes.rb
# Picks a random quote from quotes.txt and prints it to stdout.
# Quotes should be separated by double newlines (single empty lines) in the file.
# We need to define Array#sample if this version of Ruby is really old.
unless Array.respond_to? 'sample'
class Array
def sample
self[rand(length)]
@nickserv
nickserv / harmonic.rb
Created December 3, 2012 18:35
Harmonic Series in Ruby
# Set the sum to 0 initially and print its value
p sum = 0
# Set i to 0 initially
i = 1
# Start an infinite, iterative loop
loop do
# Add 1/i (as a float to keep this accurate) to sum and print its new value
p sum += 1/i.to_f
@nickserv
nickserv / Ugly.java
Created December 10, 2012 05:02
Hello World in Java... using unicode escapes
/*
* Try compiling this. It actually works.
* Output: "Hello world"
*/
\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020
\u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079
\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020
\u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063
\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028
@nickserv
nickserv / bogo.rb
Last active October 13, 2015 21:08
Bogo Sort in Ruby
class Array
def in_order?
(0..length-2).none? { |i| self[i] > self[i+1] }
end
def bogo_sort
copy = self.dup
copy.shuffle! until copy.in_order?
copy
@nickserv
nickserv / gist:4312799
Created December 16, 2012 20:51
Cheat Sheet: Git Submodules

##Download a repository with submodules

git clone URL MAIN_REPO_DIR
git submodule init
git submodule update

##Add a submodule

cd MAIN_REPO_DIR
@nickserv
nickserv / benchmark.rb
Last active December 9, 2015 19:18
Example: Ruby Benchmark
require 'benchmark'
Benchmark.bm do |b|
b.report "summation equation" do
sum = 1_000_000*(1_000_000+1)/2.to_f
end
b.report "each loop" do
sum = 0
@nickserv
nickserv / gist:4527994
Created January 14, 2013 05:56
Hello world with ed
$ ed
i
Hello, world!
.
w hello.txt
14
q
$ cat hello.txt
Hello, world!
@nickserv
nickserv / cram.rb
Created January 18, 2013 00:52
Cramming multiple expressions into one line of Ruby (hacky, useful for code golfing)
[a = 'hello'].each { |_| b = 'world' }
puts a # hello
puts b # world
@nickserv
nickserv / repl.rb
Last active December 12, 2015 02:09
A simple REPL in Ruby
# REPL: Read Evaluate Print Loop
# See http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop
# The prompt for the REPL
PROMPT = 'repl> '
# Keep reading, evaluating, and printing forever (or until the user exits)
loop do
# Display the prompt
print PROMPT