Skip to content

Instantly share code, notes, and snippets.

View khan-hasan's full-sized avatar

Hasan Khan khan-hasan

  • Delta Air Lines
  • Atlanta, GA
View GitHub Profile
@khan-hasan
khan-hasan / cat.rb
Created October 7, 2017 21:54
CF 3.4 | simple ruby Cat class
class Cat
attr_reader :color, :breed, :name
# There is no need for a separate attr_writer and separate attr_reader for name because attr_accessor can combine those methods into one (DRY)
# attr_writer :name
attr_accessor :name
def initialize(color, breed)
@color = color # Instances variables are available to other methods inside of this class. This is unlike local variables, which only exist inside the method they're called in.
@breed = breed
@hungry = true
@khan-hasan
khan-hasan / 3.5.rb
Created October 7, 2017 22:34
CF 3.5 | oop, pt. 2 (instance methods, inheritance, etc._
str.is_a?(Object)
str.class
# here we extend Ruby's string class meaning that all strings in the environment will belong to this class
class String
def star
self + " *"
end
end
@khan-hasan
khan-hasan / rock_paper_scissors.rb
Created October 10, 2017 17:40
Rock, Paper, Scissors Game
# ends the game
def end_game
puts "\nBye!"
end
# the game which recursively calls itself until user presses "X" to call end_game and finish the game
def game
puts "==============================\nPlease choose one from the following:\n(R) Rock\n(P) Paper\n(S) Scissors\n(X) End game\n\n"
user_turn = gets.chomp.capitalize!
if user_turn != "R" && user_turn != "P" && user_turn != "S" && user_turn != "X"
class AddAdminFlagToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :admin, :boolean, default: false, null: false
end
end
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
@khan-hasan
khan-hasan / new.html.erb
Created December 21, 2017 15:01
This is the form for creating new users.
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field form-group">
<%= f.text_field :first_name, autofocus: true, class: "form-control", placeholder: 'First name' %>
</div>
<div class="field form-group">
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:show, :index]
load_and_authorize_resource
# GET /users
# GET /users.json
def index
@users = User.all
end
require 'rails_helper'
describe Product do
context "when the product has comments" do
let(:product) { Product.create!(name: "race bike") }
let(:user) { User.create!(first_name: "John", last_name: "Smith", email: "[email protected]", password: "password123") }
before do
using System;
using System.Collections.Generic;
namespace Roman
{
internal class Program
{
private static void Main(string[] args)
{
// Decode and print the numerals.
public class Customer
{
string CustomerName;
// Constructor for Customer class
public Customer(string Name)
{
CustomerName = Name;
}
}