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
# This gist demonstrates - How to download data from database in excel or xls format in Ruby on Rails | |
# Tested with - Ruby 1.9.3, rails 3.2.1, db - postgres | |
class DownloadsController < ApplicationController | |
before_filter :define_header, :except => :index | |
def index | |
... | |
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
/* | |
* | 1 1 |n |Fn+1 Fn | | |
* | 1 0 | = |Fn Fn-1 | | |
* | |
* Figure 1 | |
*/ | |
public int FibonacciLogN(int n) | |
{ | |
if(n <= 0) | |
return 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
# This gist shows how to display Rails like flash messages while handling AJAX or JS requests | |
# Note - This might not be the best way to do this but it works for me | |
Step 1. Create a partial which will contain the message to be displayed | |
Example - _success.html.erb | |
<div id="flash_message"> | |
<p>Success</p> | |
</div> | |
Step 2. In the Controller, add code to call this partial on a JS request |
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
----------------------------------------------- | |
application.js | |
----------------------------------------------- | |
require [ | |
'backbone', | |
'path/to/router/with/index/handler'], | |
(Backbone, IndexRouter) -> | |
$ -> | |
Main = new IndexRouter() |
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
class Test | |
waitForTwoCalls: () -> | |
$.when(@firstCall(), @secondCall()).then () => | |
doSomeThing() | |
firstCall: () -> | |
token = $.Deferred | |
$.ajax({ | |
url: foo/bar, |
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
# Makes ajax req to fetch data | |
class Model | |
initialize(id) | |
@id = id | |
load: (token) -> | |
$.ajax({ | |
url: "foo/bar/#{@id}", | |
.... | |
success: (response) => |
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
MaxLength = N | |
valid_string = some_long_text[0,N] if some_long_text.length > N |
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 fs = require('fs'); | |
var spawnProcess = require('child_process').spawn; | |
var filename = 'some/file' | |
fs.watch(filename, function() { | |
var list = spawnProcess('ls', ['-lh', filename]); | |
var info = ''; | |
// listener get invoked every time a chunk of data is available. Data is read in chunks | |
list.stdout.on('data', function(chunk) { |
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 MongoClient = require('mongodb').MongoClient; | |
var connectionString = 'mongodb://localhost:27017/test'; | |
// connect to test db | |
MongoClient.connect(connectionString, function(err, db) { | |
if(err) throw err; | |
console.log("CONNECTED"); | |
var q = {name: 'Super Man'}; | |
// query superheroes collection and display result |
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
// Represents the node in the tree. Will be displayed as a small circle in the browser. | |
// x, y --> x, y co-ordinates of the center of circle | |
// r --> radius of the circle | |
// ctx --> context of the canvas | |
// data --> data to be displayed (Only number) | |
var Node = function(x,y,r, ctx, data) { | |
// left child of a node | |
this.leftNode = null; | |
// right child of a node |
OlderNewer