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 / 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 / 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|
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 / 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)
@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 / 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 / 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 / must.rb
Last active March 18, 2016 05:17
Extending TestCase functionality to add the more elegent must method, through some ruby metaprogramming magic
# Ruby Dose Facebook Page
require 'test/unit'
class Test::Unit::TestCase
def self.must(name, &block)
# convert "any method description" to any_method_description
test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym
class Info
class << self
def method_missing(name, *args, &block)
case name
when /^get_(.*)/
send($1, *args, &block)
else
super
end
end
@theHamdiz
theHamdiz / ruby logger.rb
Created March 9, 2016 12:35
In this gist we will try to add the logging functionality to the pre-existing Fixnum '/' method and learn how to do so in the process thro alias_method technique.
require 'logger'
# Generally its a good practice to avoid global variables, but a good use of them would be in a logger constant
# you can try other Logger creation options such as
# Keep data for the current month only
# Logger.new('this_month.log', 'monthly')
# Keep data for today and the past 20 days.
# Logger.new('application.log', 20, 'daily')
# Start the log over whenever the log exceeds 100 megabytes in size.
# Logger.new('application.log', 0, 100 * 1024 * 1024)
$LOG = Logger.new('app.log', 'monthly')