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 / Treat single object like array in ruby.rb
Created February 18, 2016 23:53
This gist describes how you can treat a single item like an array in ruby by just prefixing it with the asterisk operator(*)
class Fixnum
def digits_count
# a cool trick to return the base 10 logarithm of x.
# which always defaults to the actual number of digits - 1
Math.log10(self).to_i + 1 # here we add the missing 1.
end
end
# a single random number
item = rand(9999999)
# just a dummy array
@theHamdiz
theHamdiz / ruby join tricks.rb
Created February 19, 2016 23:37
Cool array join trick using Ruby. You would've never guessed it was that easy to accomplish!
# facebook.com/ruby.dose
# This is one of the coolest join tricks ruby can provide you...
# creating an array of string literals, multiplying this array by another
# string literal will actually cause joining this array together using
# the latter string literal
puts %w|Mohamad Abdullah Abdulmotaleb| * ' Ibn '
puts %w{Ahmad Hamdi Saad Emara} * ' Ibn '
@theHamdiz
theHamdiz / lambda_literal.rb
Last active February 23, 2016 00:15
The lambda literal symbol, which was introduced recently and is the new default way to define scopes in rails
# The lambda literal symbol ->
# with it you can store a whole
# anonymous method in a variable
# and access it later
# Introduced recently
# The new default way to define scopes in rails
one_plus_one = -> { 1 + 1 }
p one_plus_one.call
###############################################
expon = -> (v) { v ** v }
long = <<EOF
THIS IS A VERY LONG STRING
WITH MULTIPLE LINES, AND OTHER COOL STUFF
LIKE INSERTING #{"Some cool string interpolation"} AND ALSO ''
EOF
puts long
@theHamdiz
theHamdiz / include_vs_extend.rb
Last active December 29, 2016 11:38
Class methods are not included they are extended, see this gist for further explanation on the major difference between including a module and extending it.
module A
def our_method
puts "Module A"
end
end
class Including
# by including a module we mark its methods as instance methods
# that can only be used from the object scope
include A
@theHamdiz
theHamdiz / rpg.rb
Created February 29, 2016 10:49
Random Password Generator Ruby Module
module RandomPassword
def generate
# create a one big array of seeding data
seed = [('a'..'z'), ('!'..'+'), (1..9), ('A'..'Z')].map { |e| e.to_a }.flatten
# get random 16 characters from this array
original = (0..16).map { seed[rand(seed.length)] }.join
# just to be sure, randomize them once more
original.split('').shuffle.join
end
end
@theHamdiz
theHamdiz / object serialization in ruby.rb
Created March 7, 2016 16:04
very basic example of object serialization in #ruby...
class GameCharacter
def initialize(name, type, weapons)
@name = name
@type = type
@weapons = weapons
end
attr_accessor :name, :type, :weapons
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')
class Info
class << self
def method_missing(name, *args, &block)
case name
when /^get_(.*)/
send($1, *args, &block)
else
super
end
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