Skip to content

Instantly share code, notes, and snippets.

@tatey
tatey / factorial.rb
Created July 21, 2009 12:26
Factorial in Ruby
def factorial(count, sum = 1)
count.zero? ? sum : factorial(count - 1, sum * count)
end
@tatey
tatey / gist:154781
Created July 25, 2009 11:48
JavaScript Scroll
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>JavaScript Scroll</title>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.2/scriptaculous.js" type="text/javascript"></script>
<style type="text/css">
def last_but_one(ary)
if ary.size == 2
ary.first
else
last_but_one(ary.slice(1..ary.size))
end
end
require 'benchmark'
n = 20000
Benchmark.bm do |b|
b.report { n.times do; n == 1 or n == 2 or n == 3 or n == 5 or n == 6 or n == 7 or n == 8 or n == 9 or n == 10; end }
b.report { n.times do; [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].include?(n); end }
end
# $ ruby test.rb
# user system total real
# Learning Ruby. An example of a bank account implemented in Ruby.
# We're doing everything the "long" way. Much of this code
# could be expressed more consicely.
require 'test/unit'
# Account represents a person's bank account. It has a number, name and
# balance. Money can be deposited and withdrawn from the account. You
# cannot withdraw more money than what's in the account.
class Account
# Learning Ruby. A consice implementation of the Bank Account example.
# http://gist.github.com/277023
require 'test/unit'
class Account
attr_reader :number, :name, :balance
@@number = 0
h = {:a => 1, :b => 2, :c => 3}
h.each do |pair|
# pair is an Array
puts "key: #{pair.first}, value :#{pair.last}"
end
h.each do |key, value|
puts "key: #{key}, value :#{value}"
end
def fact(x)
x > 1 && x * fact(x - 1) || 1
end
puts fact(6)
f = lambda { |x| x > 1 && x * f.call(x - 1) || 1 }
puts f.call(6)
var fact = function(x) {
return x > 1 && x * fact(x - 1) || 1
}
alert(fact(6));
// Option 1
$.a = {
settings: {},
method1: function() {},
method2: function() {}
};
// Option 2
$.a = function() {};