Skip to content

Instantly share code, notes, and snippets.

View tkbeili's full-sized avatar

Tammam Kbeili tkbeili

View GitHub Profile
module CalorieCalculator
def calculate_calories(ingredients)
total_calories = 0
ingredients.each do |ingredient|
if ingredient.respond_to?(:calorie_count)
total_calories += ingredient.calorie_count
else
raise TypeError, "The ingredient must have a calorie_count"
end
end
@tkbeili
tkbeili / gist:5114217
Created March 8, 2013 04:29
my .IRBRC
# IRBRC file originally by Iain Hecker, http://iain.nl
# put all this in your ~/.irbrc
require 'rubygems' if RUBY_VERSION[2] == ?8 # rubygems is only needed in 1.8
require 'yaml'
require 'irb/completion'
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
require 'spec_helper'
describe QuestionsController do
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
require 'spec_helper'
describe "Questions" do
describe "GET /questions" do
it "Should respond with success to /quetsions" do
get questions_path
response.status.should be(200)
end
end
@tkbeili
tkbeili / One to Many Relationship
Created March 25, 2013 16:11
Steps for creating one to many relationship with SQL database in Rails
1- Decide which one is the “parent” and which one is the “child”. So the parent has_many children
2- If you are using SQL database add a column to the child ActiveRecord model named parent_id
3- In the parent ActiveRecord model add has_many :children (notice we used plural in here)
4- In the child ActiveRecord model add belongs_to :parent (notice we used singular in here)
5- (optional) Change the child controller to be nested from the parent’s controller in order to re-use methods (mostly for before_filter) by:
a- Makes the routes for the child controller nested inside the “show” action of parent controller
b- Make the child controller inherit from the parent controller.
c- Setup filters as needed
d- Change URLs relating to child controller around your app.
@tkbeili
tkbeili / gist:8423267
Last active March 16, 2016 20:38
My .irbrc
# ruby 1.8.7 compatible
require 'rubygems'
require 'irb/completion'
# interactive editor: use vim from within irb
begin
require 'interactive_editor'
rescue LoadError => err
warn "Couldn't load interactive_editor: #{err}"
end
class TriangleClimber
def initialize(size = nil, grid = nil)
@possible_ways = 0
@grid = grid ? grid.inject([]) {|r,e| r << e.strip.split(" ")}.reverse : nil
@size = size || @grid.size
end
def find_possible_climbing_ways
0.upto(@size - 1) {|x| move([0, x]) unless grid_has_trap?([0, x])}
@tkbeili
tkbeili / .irbrc
Last active August 29, 2015 14:00
# ruby 1.8.7 compatible
require 'rubygems'
require 'irb/completion'
# interactive editor: use vim from within irb
begin
require 'interactive_editor'
rescue LoadError => err
warn "Couldn't load interactive_editor: #{err}"
end
@tkbeili
tkbeili / addresses
Last active August 29, 2015 14:00
Random Addresses For Testing Purposes
2335 Canton Hwy #6 Windsor ON N8N 3N2
6 Arch St #9757 Alcida NB E8J 2C4
9547 Belmont Rd #21 Belleville ON K8P 1B3
73 Pittsford Victor Rd Vancouver BC V5Z 3K2
447 Commercial St Se LIle-Perrot QC J7V 4T4
47 Garfield Ave Swift Current SK S9H 4V2
3 Mill Rd Baker Brook NB E7A 1T3
136 W Grand Ave #3 Delhi ON N4B 1C4
80 Maplewood Dr #34 Bradford ON L3Z 2S4
58 Hancock St Aurora ON L4G 2J7
@tkbeili
tkbeili / fizzbuzz.rb
Last active August 29, 2015 14:01
FizzBuzz Solution
for number in 1..100
if number % 3 == 0 && number % 5 == 0
puts "FIZZBUZZ"
elsif number % 3 == 0
puts "FIZZ"
elsif number % 5 == 0
puts "BUZZ"
else
puts number
end