Skip to content

Instantly share code, notes, and snippets.

View aghyad's full-sized avatar
🤖
Never give up

Aghyad Saleh aghyad

🤖
Never give up
View GitHub Profile
@aghyad
aghyad / quick_sort.rb
Last active December 21, 2015 00:58
Algorithms in Ruby: Quick Sort
class Array
def aghyad_quick_sort
return self if length <= 1
pivot = self[length/2]
return find_all { |i| i < pivot }.quick_sort + find_all { |i| i == pivot } + find_all { |i| i > pivot }.quick_sort
@aghyad
aghyad / radix_sort.rb
Last active December 21, 2015 00:59
Algorithms in Ruby: Radix Sort
class Array
def aghyad_radix_sort
round = 0
bucket = [[],[],[],[],[],[],[],[],[],[]] #bucket of 10 sub-arrays
self.each do |x|
bucket[x.to_s.split('')[-1*(round+1)].to_i] << x
end
a = bucket.flatten
@aghyad
aghyad / counting_sort.rb
Created August 13, 2013 19:53
Algorithms in Ruby: counting sort (a variant of the radix sort)
class Array
def aghyad_counting_sort
count = []
a = self
puts a.inspect
m = a.first
a.each do |x|
m = x if m < x
module BinaryTree
class Node
attr_reader :word, :count, :left, :right
include Enumerable
def initialize(word)
@word, @count = word, 1
end
@aghyad
aghyad / Ruby2_and_DTrace.txt
Created December 3, 2013 22:38
Experimenting with Ruby 2.0 and DTrace scripts
Experimenting with Ruby 2.0 and DTrace scripts
I was very excited when I heard about the new Ruby 2.0 features. I am not going to delve into Ruby 2.0 features here, but I will talk about one feature I consider the best of Ruby 2: integration of DTrace.
I've never used DTrace before, much less I thought it can be used efficiently with Ruby the same way debugger is being used with ruby on rails.
DTrace has been for years the most famous tracing framework and now it's time to start using it with Ruby 2. So, after readings about DTrace, I tried to put it in action. I will be using Mac OS X 10.7.
Installing Ruby 2.0
@aghyad
aghyad / installing_passenger_nginx_ror_on_ubuntu_without_rvm_rbenv
Last active March 19, 2018 10:42
A quick tutorial on installing passenger, Nginx, Ruby and Rails on Ubuntu 12.04 (without RVM/without rbenv)
When installing passenger, Nginx, Ruby and Rails on Ubuntu 12.04 (without RVM/without rbenv):
First: installing Ruby (without RVM):
sudo apt-get update
sudo apt-get install ruby1.9.1 ruby1.9.1-dev \
rubygems1.9.1 irb1.9.1 ri1.9.1 rdoc1.9.1 \
build-essential libopenssl-ruby1.9.1 libssl-dev zlib1g-dev
@aghyad
aghyad / validate_email_dns.rb
Created December 3, 2013 22:42
Validating email dns in ruby
require 'resolv'
def validate_email_domain(email)
domain = email.match(/\@(.+)/)[1]
Resolv::DNS.open do |dns|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX) + dns.getresources(domain, Resolv::DNS::Resource::IN::A)
end
@mx.size > 0 ? true : errors.add(:email, "Invalid email id")
end
@aghyad
aghyad / some_useful_bash_profile_aliases
Created December 3, 2013 22:47
Some useful bash profile aliases
alias glog="git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias mycukes= 'RAILS_ENV="development" bundle exec cucumber'
alias myspecs= 'RAILS_ENV="development" bundle exec rspec spec'
alias vim='vim +NERDTree'
alias rspecc='clear && bundle exec rspec'
alias drspecc='clear && RAILS_ENV=development bundle exec rspec'
alias mygit='git config --global user.name "SOME-NAME-HERE" && git config --global user.email SOME-EMAIL-HERE && echo "************" && echo "switched git user name to:" && git config user.name && echo "and git user email to:" && git config user.email && echo "************"'
@aghyad
aghyad / how_to_work_with_more_than_one_user_and_more_than_one_key.txt
Created December 3, 2013 22:58
how to work with more than one user and more than one key
To be able to utilize more than one key in your github environment on one computer, and assuming we have the 2 following keys:
~/.ssh/id_rsa_personal_key
~/.ssh/id_rsa_company_key
Create the file ~/.ssh/config with the follwoing content:
### Default (Personal) GitHub
Host github.com
HostName github.com
User git
@aghyad
aghyad / installing_ruby2_rails4_with_rbenv_nginx_passenger_ubuntu_12.04.txt
Last active December 30, 2015 06:49
Installing ruby2 rails4 with nginx passenger WITH rbenv on ubuntu 12.04
If you have a packaged ruby / rails / rvm / rbenv etc installed, get rid of them all, eg:
$ ruby --version
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
$ sudo apt-get remove ruby
if that didn't work, you can do:
$ aptitude purge ruby