Skip to content

Instantly share code, notes, and snippets.

View descartez's full-sized avatar

Ryan Stong descartez

View GitHub Profile
@XanderStrike
XanderStrike / fibs.rb
Last active August 29, 2015 14:23
fibs.rb
# ruby fibs
require 'benchmark'
# === stupid ===
def recursive_fib(n)
n <= 1 ? n : recursive_fib(n-1) + recursive_fib(n-2);
end
puts Benchmark.measure { recursive_fib(35) }
@creature
creature / linkedlist.rb
Last active June 10, 2020 07:52
A Ruby demo of a linked list, transformed into a doubly-linked list and some performance improvements.
class Node
attr_accessor :value
attr_accessor :next, :previous
def initialize(value)
@value = value
@next = nil
@previous = nil
end
end
@RohitRox
RohitRox / sinatra app skeleton generator
Created January 19, 2013 19:22
Simple bash function to generate sinatra app skeleton. USAGE: $ sinatra app_name; shotgun
function sinatra {
if [ "$1" == "" ]; then
echo "Usage:"
echo "'sinatra app_name' to create a new Sinatra app skeleton";
else
if [ ! -d "$1" ]; then
echo "Generating your app ... "
mkdir $1
mkdir $1/views;
@runemadsen
runemadsen / app.rb
Created October 17, 2012 13:45
Sinatra File Upload
require 'sinatra'
get "/" do
erb :form
end
post '/save_image' do
@filename = params[:file][:filename]
file = params[:file][:tempfile]
@amejiarosario
amejiarosario / rails_activerecord_cheatsheet.md
Created June 18, 2012 22:18
Rails ActiveRecord (association) - Cheatsheet

Rails ActiveRecord (association) - Cheatsheet

http://guides.rubyonrails.org/association_basics.html

Types of associations

  • belongs_to : TABLE_NAME
  • has_one : TABLE_NAME [:through => :TABLE_NAME]
  • has_many : TABLE_NAME [:through => :TABLE_NAME]
  • has_and_belongs_to_many : TABLE_NAME [:join_table => :TABLE_NAME]
@JoshCheek
JoshCheek / 1-fizz_buzz.rb
Created December 11, 2011 18:47
Ruby Code Golf solutions
puts Solution.new('1. Fizz Buzz', <<SOLUTION)
def fizzbuzz(n)(n%15==0?'FizzBuzz':n%3==0?'Fizz':n%5==0?'Buzz':n).to_s end
SOLUTION
.test { fizzbuzz 3 }.expect { |result| result == "Fizz" }
.test { fizzbuzz 10 }.expect { |result| result == "Buzz" }
.test { fizzbuzz 45 }.expect { |result| result == "FizzBuzz" }
.test { fizzbuzz 31 }.expect { |result| result == "31" }