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 / 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 / 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 / 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
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 / 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 }
@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 / 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 / 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')
@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
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