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 / gist:e280cc44523956adf4ec
Created January 12, 2015 09:59
Error building ruby on mac os x Yosemite 10.10.1
rvm install 2.2.0
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.10/x86_64/ruby-2.2.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
Requirements installation successful.
Installing Ruby from source to: /Users/a7madx7/.rvm/rubies/ruby-2.2.0, this may take a while depending on your cpu(s)...
ruby-2.2.0 - #downloading ruby-2.2.0, this may take a while depending on your connection...
ruby-2.2.0 - #extracting ruby-2.2.0 to /Users/a7madx7/.rvm/src/ruby-2.2.0....
@theHamdiz
theHamdiz / Complete error
Created January 12, 2015 10:05
Complete error
Ahmads-Mac-Pro:~ a7madx7$ cat /Users/a7madx7/.rvm/log/1421056561_ruby-2.2.0/install.log
[2015-01-12 11:58:33] __rvm_make
__rvm_make ()
{
\make "$@" || return $?
}
current path: /Users/a7madx7/.rvm/src/ruby-2.2.0
PATH=/usr/local/opt/pkg-config/bin:/usr/local/Cellar/libtool/2.4.2/bin:/usr/local/opt/automake/bin:/usr/local/opt/autoconf/bin:/Users/a7madx7/.rbenv/shims:/Users/a7madx7/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/a7madx7/.rvm/bin
command(2): __rvm_make install
++ make install
@theHamdiz
theHamdiz / libyaml installation error
Created January 12, 2015 10:12
libyaml installation error
Ahmads-Mac-Pro:~ a7madx7$ rvm remove all
Are you SURE you wish to 'remove' all rubies?
(anything other than 'yes' will cancel) > yes
ruby-2.2.0 - #removing src/ruby-2.2.0..
ruby-2.2.0 - #removing rubies/ruby-2.2.0..
Ahmads-Mac-Pro:~ a7madx7$ rvm pkg install libyaml
Beware, 'rvm pkg ...' is deprecated, read about the new autolibs feature: 'rvm help autolibs'.
Checking requirements for osx.
@theHamdiz
theHamdiz / rename files in directory
Created February 23, 2015 11:54
Rename files in any given directory
puts 'Renaming files...'
# a parameter to the program from command line.
folder_path = ARGV[0] # /Users/a7madx7/Pictures/WallPapers
Dir.glob(folder_path + "/*").sort.each_with_index do |f, i|
# prepare a the brand new 'renamed' complete file path.
new_name = File.dirname(f) + '/' + i.to_s + File.extname(f)
# do the actual renaming of the file.
@theHamdiz
theHamdiz / zip.rb
Created July 25, 2015 20:31
.zip method
first_names = ["Ahmad", "Ahmad", "Emad"]
last_names = ["Hamdi", "Magdi", "Elsaid"]
first_names.zip(last_names).inspect
@theHamdiz
theHamdiz / bubble sort.rb
Created July 25, 2015 20:36
Bubble sort simple sorting algorithm
def bubble_sort(array)
n = array.length
loop do
swapped = false
(n-1).times do |i|
# if the item that comes first in the array is bigger than that comes next
# then resort it
if array[i] > array[i+1]
array[i], array[i+1] = array[i+1], array[i]
swapped = true
@theHamdiz
theHamdiz / sorted_list.rb
Last active February 11, 2016 00:16
‪#‎Implement‬ your own sorted list in ‪#‎ruby‬, and how to use predefined SortedSet class.
require 'set'
require 'forwardable'
set = SortedSet.new([8,6,4,2,0,5,9,66,8])
p set # => #<SortedSet: {0, 2, 4, 5, 6, 8, 9, 66}>
# How to implement the same 'SortedSet' functionality on your own
class SortedList
include Enumerable
extend Forwardable
module OS
def windows?
# use !object.nil? instead of object != nil
!(/mingw|mswin|cygwin|bccwin|emx/ =~ RUBY_PLATFORM).nil?
end
def mac?
# check for the darwin kernel name
!(/darwin/ =~ RUBY_PLATFORM).nil?
end
@theHamdiz
theHamdiz / tap method.rb
Created February 17, 2016 00:26
Use the #tap method to initialize your objects beautifully and elegantly instead of setting various properties manually.
class User
attr_accessor :a, :b, :c
end
# basically the tap method yields the calling object
# to the block and returns it back
user = User.new.tap do |u|
u.a = 1
u.b = 2
u.c = 3
@theHamdiz
theHamdiz / double star.rb
Created February 18, 2016 07:57
Advanced #ruby method signatures with the double star operator.
# a is a regular parameter, the only first parameter
# *b is whatever comes after 'a' grouped in an array
# **c is any named: :argument
def my_method(a, *b, **c)
p a, b, c
end
my_method(3, 5, 7, 9, atom: 'is a text editor', ruby: 'is awesome')