Skip to content

Instantly share code, notes, and snippets.

View sumanmukherjee03's full-sized avatar

Suman Mukherjee sumanmukherjee03

View GitHub Profile
@sumanmukherjee03
sumanmukherjee03 / random_string.rb
Created July 2, 2011 17:50
Generate random strings in ruby 1.9
[0..9, ?a..?z, ?A..?Z].map(&:to_a).flatten.sample(10).join #size has been hard coded as 10 here
@sumanmukherjee03
sumanmukherjee03 / mess_with_bindings.rb
Created September 20, 2012 12:08
Messing with bindings in ruby
# Coding hazard.
class CoOrdinate
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
end
@sumanmukherjee03
sumanmukherjee03 / class_var_ex.rb
Created October 19, 2012 00:17
Messing with class variables in ruby
# Execute this file as follows and compare the results in ruby 1.8.*:
#
# ruby class_var_ex.rb
# ruby class_var_ex.rb "include module"
# ruby class_var_ex.rb "include module" "include global"
# ruby "include global"
@@test = 9 if ARGV.last == "include global"
module X
@sumanmukherjee03
sumanmukherjee03 / gist:5111041
Created March 7, 2013 19:33
times prototype for Number
Number.prototype.times = function(str) {
var result = "", i = this;
while(i-- > 0) {result += str;}
return result;
};
@sumanmukherjee03
sumanmukherjee03 / gist:5111322
Created March 7, 2013 20:06
Closure example
// Exercise 2 - Closures
// Wrap the following code in a closure and export only the "countdown" function.
// Code
var countdown = function() {
var index;
function log(){
@sumanmukherjee03
sumanmukherjee03 / gist:5117225
Created March 8, 2013 15:30
Exercise or cars
function logCar(obj) {
console.log("I'm a " + obj.color + " " + obj.make);
}
var Car = function(make, color){
this.make = make;
this.color = color;
};
Car.prototype = (function(){
@sumanmukherjee03
sumanmukherjee03 / gist:5117968
Created March 8, 2013 16:59
cache function
Function.prototype.cache = (function() {return {};})();
Function.prototype.setCache = (function() {
var self = this;
return function(key) {
if(!this.cache[key])
this.cache[key] = this.call(self, key);
};
})();
var Person = function(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var thomas = new Person('Thomas');
var amy = new Person('Amy');
@sumanmukherjee03
sumanmukherjee03 / implicit_return_encapsulation_issues.rb
Created March 9, 2013 23:59
Does implicit return in ruby violate encapsulation?
class Outer
class Inner
def initialize(attrs)
@obj = attrs
end
def do_something
# do somehting with the @obj
@obj
end
@sumanmukherjee03
sumanmukherjee03 / hash_ext.rb
Created May 21, 2013 19:50
Hash decoration with custom method for finding value for any nested key
require 'rspec'
RSpec.configure do |config|
config.mock_with :rspec
config.color_enabled = true
end
module MyHashTools
def get_val_for(sym)