Skip to content

Instantly share code, notes, and snippets.

View kedarmhaswade's full-sized avatar
💭
Just trying to catch up with my calendar

Kedar Mhaswade kedarmhaswade

💭
Just trying to catch up with my calendar
  • JyMob
  • Sunnyvale, USA
View GitHub Profile
@kedarmhaswade
kedarmhaswade / one-liners.rb
Created December 17, 2010 19:20
Supposed to interesting, concise, clear and useful one-liners
# find methods of an Array that have ! equivalents: e.g. map (Note: the method /methods/ returns an Array of /Symbol/s)
[].methods.delete_if{|x| [].methods.index((x.to_s+"!").to_sym) == nil}
# => [:reverse, :rotate, :sort, :collect, :map, :select, :reject, :slice, :uniq, :compact, :flatten, :shuffle, :sort_by]
# find methods of a String that have ! equivalents: e.g. sub
"".methods.delete_if{|x| "".methods.index((x.to_s+"!").to_sym) == nil}
#=> [:succ, :next, :upcase, :downcase, :capitalize, :swapcase, :reverse, :sub, :gsub, :chop, :chomp, :strip, :lstrip, :rstrip, :tr, :tr_s, :delete, :squeeze, :slice, :encode]
@kedarmhaswade
kedarmhaswade / Foo.rb
Created January 9, 2011 05:01
Example class showing usage of yardoc ...
#!/usr/bin/env ruby
class Foo
# This is m1. It gets documented correctly.
# @param [Integer] id an integer representing unique ID
# @param [String] name string representing the name
# @see #m2
def m1(id, name)
end
@kedarmhaswade
kedarmhaswade / gist:2506938
Created April 27, 2012 07:30
rake -T and :git on gem in Gemfile ...
source 'https://rubygems.org'
# details omitted ...
#1
gem 'rdoc_rest', :git => 'git://github.com/kedarmhaswade/rdoc_rest.git'
#2
#gem 'rdoc_rest'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# Demo::Rack::ApiAccess is a rack middleware and implements the rack interface.
# Its purpose is to validate the API access. In essence, it "logs in" the user for which
# the current request has an access_token.
require 'oauth2/provider/models/active_record/access_token'
module Demo::Rack
class ApiAccess
def initialize(app)
@app = app
end
@kedarmhaswade
kedarmhaswade / rails-3.2-show-exceptions
Created May 10, 2012 20:20
The new way of showing/rendering all exceptions in Rails 3.2+
# put this in an initializer
# a Rails 3.2 addendum for: http://accuser.cc/posts/1-rails-3-0-exception-handling
class MyShowExceptions
def initialize()
@@rescue_responses = Hash.new(:internal_server_error)
@@rescue_responses.update({
'ActionController::RoutingError' => :not_found,
'AbstractController::ActionNotFound' => :not_found,
'ActiveRecord::RecordNotFound' => :not_found,
@kedarmhaswade
kedarmhaswade / error_displayer.rb
Created November 5, 2012 17:59
Ensuring proper order of rack middlewares
MyApp::Application.config.middleware.insert_before ActionDispatch::ShowExceptions, ExceptionNotifier,
:sender_address => "sender@gmail.com",
:exception_recipients => "receiever@gmail.com",
MyApp::Application.config.middleware.insert_after ExceptionNotifier, ActionDispatch::ShowExceptions
require 'action_dispatch/middleware/show_exceptions'
module ActionDispatch
class ShowExceptions
private
def render_exception_with_template(env, exception)
rm -f
(in /home/kedar/.rvm/src/rbx-head)
** Invoke install (first_time)
** Invoke build:build (first_time)
** Invoke build:llvm (first_time)
** Execute build:llvm
** Invoke vm/vm (first_time)
** Invoke vm/gen/revision.h (first_time)
** Execute vm/gen/revision.h
/home/kedar/.rvm/rubies/ruby-1.9.2-head/bin/ruby vm/codegen/config_vars.rb vm/gen/config_variables.h
@kedarmhaswade
kedarmhaswade / gist:5181460
Created March 17, 2013 13:16
Making data-remote=true link behave as such, but only once!
var j = jQuery.noConflict();
j("body").append("<%= escape_javascript(render :partial => 'problems/problem_gallery_carousel')%>");
j("#show-problem-gallery-link").unbind();
j("#show-problem-gallery-link").attr("data-remote", "false"); //no Ajax anymore
j("#show-problem-gallery-link").click(function() { //attach a JS-only event handler
j("#problem-gallery-carousel").carousel({interval: false});
j("#problem-gallery-modal").modal('show');
});
@kedarmhaswade
kedarmhaswade / truncate-from-outside.c
Last active December 18, 2015 19:49
Truncation of a unix file from outside ...
/* Can we truncate a file from outside? */
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char** argv) {
/* opens the given argv[1] and writes a statement to it every 5 seconds */
int fd;
@kedarmhaswade
kedarmhaswade / DiningPhilosophers.java
Created October 6, 2013 12:28
First Implementation of the Dining Philosophers where they are deadlock prone. For instance, you could easily get into a situation where all philosophers hold left fork and are waiting to catch hold of right one => deadlock.
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
public class DiningPhilosophers {
volatile static boolean done;
final private Integer[] chopsticks;
public DiningPhilosophers(int n) {
chopsticks = new Integer[n];
for (int i = 0 ; i < n ; i++)