Skip to content

Instantly share code, notes, and snippets.

View theHamdiz's full-sized avatar
🎯
Focusing

Ahmad Hamdi theHamdiz

🎯
Focusing
View GitHub Profile
@theHamdiz
theHamdiz / flexible interface.rb
Last active April 17, 2018 15:33
Learn how to build flexible interfaces using instance_eval in #ruby
class Person
def name(n)
@name = n
self
end
def age(a)
@age = a
self
end
@theHamdiz
theHamdiz / slice a hash.rb
Created April 6, 2016 11:48
Select only the attributes you want from a hash.
# Select only the attributes you want from a hash.
# this is crazy useful when getting web parameters for example.
# ---------------------------------------------------------------------
require 'active_support/core_ext'
bacon = { delicious: true, color: "red" }
bacon.slice(:color)
# => { color:"red" }
# it returns a new hash, you can slice unknown keys and it still works.
# there's a lot of great stuff in active_support (from the rails gem)
# it mostly stands by itself but it's big so you can require just 'active_support/core_ext'
@theHamdiz
theHamdiz / fridays the 13ths.rb
Last active May 4, 2016 06:29
Get all fridays the 13ths from now on...
require 'date'
lazy_dates = (Date.today..Date.new(9999)).lazy
fridays_the_13th = lazy_dates.select { |d| d.friday? and d.day == 13 }.first(10)
puts fridays_the_13th
@theHamdiz
theHamdiz / taggable concern.rb
Created April 23, 2016 22:18
create a sample concern in rails ex. taggable concern
module Taggable
extend ActiveSupport::Concern
included do
has_many :taggings, as: :taggable, dependent: :destroy
has_many :tags, through: :taggings
end
def tag_names
tags.map(&:name)
Product.joins(:categories).where(category: { id: [1,2,3,4,5] }).where('products.name like ?', "%ant%")
Product Load (0.5ms) SELECT "products".* FROM "products" INNER JOIN "product_categories" ON "product_categories"."product_id" = "products"."id" INNER JOIN "categories" ON "categories"."id" = "product_categories"."category_id" WHERE "category"."id" IN (1, 2, 3, 4, 5) AND (products.name like '%ant%')
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: category.id: SELECT "products".* FROM "products" INNER JOIN "product_categories" ON "product_categories"."product_id" = "products"."id" INNER JOIN "categories" ON "categories"."id" = "product_categories"."category_id" WHERE "category"."id" IN (1, 2, 3, 4, 5) AND (products.name like '%ant%')
@theHamdiz
theHamdiz / insertion sorting in ruby.rb
Created June 12, 2017 05:57
insertion sorting in ruby at its finest, using traditional ruby methods and performing on a 256 digit array.
require_relative 'random_password'
include RandomPassword
class Array
def insertion_sort
start_time = Time.now
arr = self
for i in (1...(arr.size))
if arr[i-1] > arr[i]
i.downto(1) do |el|
@theHamdiz
theHamdiz / birthday_hijri.rb
Created June 12, 2017 06:54
know your birthday in the hijri calendar
# a gem created by github user ecleel.
require 'hijri'
t = Time.new(1993, 8, 23, 21, 0, 0, "+02:00")
d = DateTime.new(t.year, t.month, t.day, t.hour, t.min, t.sec, Rational(t.gmt_offset / 3600, 24))
h = d.to_hijri
puts "You were born on #{h.strftime('%c')}"
# الاثنين الموافق 5 من ربيع الأول لعام 1414 هجريه
@theHamdiz
theHamdiz / selection_sort.rb
Last active June 12, 2017 07:06
Best way to perform selection sorting in ruby, a not so recommended algorithm for large sets of data.
require_relative 'random_password'
include RandomPassword
class Array
def selection_sort
start_time = Time.now
# Selection sort (very slow on large lists)
a = self
# get the number of indices in the array
n = a.size - 1
@theHamdiz
theHamdiz / my_next_birthdays.rb
Created July 8, 2017 05:14
Get to know your next 'n' number of birthdays including the name of the day its occurring on.
require 'date'
def my_next_n_birthdays(number_of_years = 60, birthday = 24, birth_month = 8)
lazy_dates = (Date.today..Date.new(9999)).lazy
my_next_birthdays = lazy_dates.select { |d| d.day == birthday and d.month == birth_month }.first(number_of_years).map { |date| "#{date.strftime('%A')} - #{date}"}
end
puts my_next_n_birthdays
@theHamdiz
theHamdiz / Letter frequency calculator.rb
Created December 24, 2017 20:04
Ruby script to calculate the frequency of appearance of specific characters in a text, as well as mentioning the letters that were excluded by the author
puts 'Provide the sentence you want to perform letter frequency on'
text = gets.chomp # gets the sentence to iterate
puts "Performing on: #{text[0..6]}..."
text.downcase!
freqs = {} # creating a hash to iterate 'text'
freqs.default = 0
count = 0 # number of charachters in 'text'
# iterating through text
text.each_char do |char|