Skip to content

Instantly share code, notes, and snippets.

View samiron's full-sized avatar
🏠
Working from home

Samiron paul samiron

🏠
Working from home
View GitHub Profile
@samiron
samiron / testing.rb
Created March 7, 2010 05:11
A simplified demo on using attr_writer(setter method). Here the attr_reader(getter method) is not used so you need to define a method to get the instance variable.
class Human
attr_writer :name
def name
return @name
end
end
me = Human.new
me.name = 'samiron'
puts me.name
@samiron
samiron / decorator.rb
Created May 9, 2010 06:40
A code example of decorator pattern in Ruby. The example is taken from Head First Design Pattern book.
module AbstractBeverage
def cost
raise Exception, %Q|You should implement the cost for #{self.class}|
end
end
class DarkRoast
include AbstractBeverage
COST = 0.99
def cost
@samiron
samiron / application.rb
Created September 11, 2012 04:10
Sample of Rails config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'rails_autolink' #THIS IS THE PLACE YOU NEED TO ADD
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
@samiron
samiron / comments_controller.rb
Created September 19, 2012 07:26
Sample Rails Controller
class CommentsController < ApplicationController
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @comments }
end
@samiron
samiron / rails-2.3.2-example.rb
Created October 2, 2012 19:06
Rails 2.3.2 Routing Example
#config/routes.rb
map.route_a 'my_controller', :controller => "Users", :action => "a"
map.route_b 'my_controller/uid/:uid', :controller => "Users", :action => "b"
#Generated Links
route_a_url() ---> http://localhost:3000/my_controller
route_b_url(:uid => 10) ---> http://localhost:3000/my_controller/uid/10
#Requesting to the above url redirects two different actions
#Output of rake routes
@samiron
samiron / cities_controller.rb
Created October 5, 2012 18:24
Rails 3: Nested form_for child field_for parent
#Description: We want to show a region(parent) form
#while showing City information.
#If the form submitted with Region information,
# - New region will be created
# - City#region_id will have the new region id
def show
@city = City.find(params[:id])
@samiron
samiron / cities_controller.rb
Created October 8, 2012 16:32
Rails 3: Ajax Link
#FILE: app/controllers/cities_controller.rb
class CitiesController < ApplicationController
#When you do a ajax request the respond handler is ':js'
#So format.js will work. To render a response the new.js.erb
#file will be used.
def new
@city = City.new
respond_to do |format|
@samiron
samiron / user.rb
Created October 10, 2012 16:52
Rails 3: has_many :through
#FILE: models/user.rb
@samiron
samiron / application.js
Created October 14, 2012 16:42
Rails 3: Update fields via ajax
/*
This part of javascript needs to be included to make
a ajax call onchanging the selection value of dropdown.
Here, application.js is shown just for example. You should
load this based on your requirements.
*/
//Use the id of your dropdown instead of "dropdown_id"
$(function($) {
@samiron
samiron / 01_PerlParameters.rb
Created November 9, 2012 04:02
Play with Perl Parameters
sub sum{ doit('+', @_) }
sub fact{ mul(1..shift) }
sub mul{ doit('*', @_) }
sub doit
{
my ($d, @nums) = @_;
return eval( join($d, @nums) );
}