This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(count, sum = 1) | |
count.zero? ? sum : factorial(count - 1, sum * count) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def last_but_one(ary) | |
if ary.size == 2 | |
ary.first | |
else | |
last_but_one(ary.slice(1..ary.size)) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fact = function(x) { | |
return x > 1 && x * fact(x - 1) || 1 | |
} | |
alert(fact(6)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Option 1 | |
$.a = { | |
settings: {}, | |
method1: function() {}, | |
method2: function() {} | |
}; | |
// Option 2 | |
$.a = function() {}; |