Skip to content

Instantly share code, notes, and snippets.

@smsohan
smsohan / static_method_example.rb
Created August 15, 2011 17:13
unit test static methods?
def a_good_method
Logger.log(self, "This is a call to a static log method")
# do some other thing here
end
@smsohan
smsohan / extend_module.rb
Created September 16, 2011 22:02
extend_module.rb
#!/usr/bin/ruby
module BaseModule
def cry
p "wa wa wa waaa"
end
end
module ExtendedModule
include BaseModule
@smsohan
smsohan / CSharpAsyncAwait.cs
Created September 16, 2011 22:11
C# async and await example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
namespace AsyncCSharp
{
class Program
@smsohan
smsohan / hardcoded_url.js
Created September 23, 2011 17:43
Hardcoding URL in JS is risky
$.ajax({
url: '/blog/posts/' + tag_name.
type: 'DELETE',
...
});
@smsohan
smsohan / soft_delete.rb
Created September 28, 2011 21:20
An example where soft delete is a neccessity
#example code to cancel subscription
Customer.find('John Fake').tv_channel_subscriptions.find(:nat_geo).cancel!(1.month.from_now)
class Customer
attr_accessor :name, :start_date, :end_date
has_many :tv_channel_subscriptions
end
class TvChannelSubscription
@smsohan
smsohan / InstanceOf.java
Created November 2, 2011 01:46
Example of poor use of the instanceof operator
public void processMessage(Object message){
if(message instanceof CleanFloor){
((CleanFloor) message).cleanKitchen();
}
elsif(message instanceof LaunchRocket){
((LaunchRocket) message).shootToMoon();
}
}
@smsohan
smsohan / NoInstanceOf.java
Created November 2, 2011 01:57
Without using instanceof
public void processMessage(CleanFloor cleanFloor){
cleanFloor.cleanKitchen();
}
public void processMessage(LaunchRocket launchRocket){
launchRocket.shootToMoon();
}
@smsohan
smsohan / align.html
Created November 13, 2011 23:56
Example CSS for aligning a div inside another one!
<html>
<head>
<style>
.container{
width: 10%;
height: 10%;
float: left;
background-color: green;
border: 1px solid #00FF 00;
<% products.each do |product| %>
<div class="product" id="product_<%= product.id %>">
<%= link_to_image product.thumbnail, product %>
<%= product.name %>
...
</div>
<% end %>
$('.div.product').click(showScreenshots)
showScreenshots = function(){
selectedProductId = $(this).id.split('_')[1]
//load the screenshots for the product with id = selectedProductId
}