Skip to content

Instantly share code, notes, and snippets.

@smsohan
smsohan / convert_strings.rb
Created June 13, 2012 23:00
Ruby example for converting strings into primitive types
boolean_converter = ->(s){ s == "true" ? true : false}
type_converters = {Fixnum => ->(s){s.to_i}, Float => ->(s){s.to_f}, Date => ->(s){s.to_date},
TrueClass => boolean_converter, FalseClass => boolean_converter, String => ->(s){s}}
puts type_converters[Fixnum]["100"]
puts type_converters[Float]["100"]
puts type_converters[Date]["100"]
puts type_converters[TrueClass]["true"]
puts type_converters[FalseClass]["true"]
@smsohan
smsohan / module_class_test.rb
Created May 31, 2012 22:04
Is this valid?
#!/usr/bin/ruby
module Content
module Base
end
end
class Content
include Content::Base
@smsohan
smsohan / example_code.rb
Created May 22, 2012 16:49
API Doc from Tests
#Example Spec, cities_controller_spec.rb
require 'spec_helper'
describe CitiesController do
include Apify
describe 'cities api' do
@smsohan
smsohan / vehicle.coffee
Created March 30, 2012 15:29
backbonejs example
Vehicle = Backbone.Model.extend({defaults: { make: '', model: ''}})
VehicleView = Backbone.View.extend({
initialize: () ->
this.model.bind 'change', this.render, this
render: () ->
this.$el.html(this.template(this.model.toJSON()))
this
})
//HTML
<% products.each do |product| %>
<div class="product" data-id="<%= 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
}
<% products.each do |product| %>
<div class="product" id="product_<%= product.id %>">
<%= link_to_image product.thumbnail, product %>
<%= product.name %>
...
</div>
<% end %>
@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;
@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 / 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();
}
}