Skip to content

Instantly share code, notes, and snippets.

@santosh79
santosh79 / const_missing_example.rb
Created November 18, 2009 17:57
const_missing basic example
class Module
original_c_m = instance_method(:const_missing)
define_method(:const_missing) do |name|
if name.to_s =~ /^U([0-9a-fA-F]{4})$/
[$1.to_i(16)].pack("U*")
else
original_c_m.bind(self).call(name)
end
end
@santosh79
santosh79 / sha1_digest_of_a_file.rb
Created November 23, 2009 23:55
sha1 of a file
require 'digest/sha1'
Digest::SHA1.hexdigest(File.read('PATH_TO_FILE'))
irb -r my_sinatra_app.rb
ps -axcr -o "pid,pcpu,pmem,time,comm"
def outer
start = 0
anon_fun = lambda {
while start < 15 #The lambda has access to the start variable from the outer scope
puts "start: #{start}"
start += 1
sleep 2
end
}
anon_fun.call
<html>
<head></head>
<body>
<script type="text/javascript">
//This example shows how to create two functions that share state
var MYAPP = {}; //new object
MYAPP.Trivia = (function() {
var start = 0;
return {
//Adds a 'create' function to Object for creating prototypal inheritance
if (typeof Object.create !== 'function') {
Object.create = function(o) {
function F(){}
F.prototype = o;
return new F();
};
}
var singleton = (function() {
var private_variable = 10;
function private_func() {
return "this is a private function";
}
return {
public_method_one: function() {
//Change the private variable
function base(id) {
return {
id: id,
toString: function() {
return "base: "+this.id;
}
};
}
at_exit { puts "exit 1" }
at_exit { puts "exit 2" }
#2 runs before 1 -- reverse order of execution