Skip to content

Instantly share code, notes, and snippets.

View mehdi-farsi's full-sized avatar

Mehdi FARSI mehdi-farsi

View GitHub Profile
@mehdi-farsi
mehdi-farsi / string.rb
Last active August 29, 2015 14:08
Ruby Trick To Extract an Email From a String Using Regex
# The goal of this code snippet isn't to make an email validator..
# So the REGEX is not steady but works for simple cases.
#
# You can extract whatever you want by changing the Regex. :)
line = "Congratulation ! here is your email : mehdi-farsi@long-and_boring-company_name.com. Are you happy ?"
email = line[/(\w|[\.\-])+@(\w|[\.\-])+\.[a-zA-Z]+/]
@mehdi-farsi
mehdi-farsi / composition_over_inheritance.rb
Created November 11, 2014 00:24
Example of "Composition Over Inheritance" Concept
class Vehicle; end
class Car < Vehicle
def initialize
# A composition to separate standalone Engine class.
# That permit to create a class Bicycle without give it
# the property of an engine Vehicle.
@engine = GasolineEngine.new
end
@mehdi-farsi
mehdi-farsi / modules_and_classes.rb
Last active August 29, 2015 14:09
Ruby Method-Calling Hierarchy Between Modules and Classes
# Ruby Method-Calling Hierarchy.
#
# When a method is called Ruby :
#
# 1 - searches the method in class.
#
# 2- searches the method in each included modules
# from the last one to the first one.
#
# Study Case:
@mehdi-farsi
mehdi-farsi / my_flatten.rb
Last active March 10, 2020 20:02
A not efficient but easy-to-code implementation of Array#flatten
# Flat the array layer by layer
#
# Insights:
#
# - Using recursion.
#
class Array
def my_flatten(depth = nil)
if depth.nil? || depth.is_a?(Fixnum)
@mehdi-farsi
mehdi-farsi / proc_block_lambda.rb
Created November 17, 2014 23:36
Difference between Procs, Blocks, Lambdas in Ruby
#
# Main difference: Procs are objects, blocks are not.
#
# The '&' tells ruby to turn the proc into a block.
#
class Array
def map2
new_ary = []
self.each do |elem|
@mehdi-farsi
mehdi-farsi / head.rb
Created November 19, 2014 15:03
Ruby Net::HTTP Get header without body from an HTTP request
require "net/http"
uri = URI('YOUR URL')
req = Net::HTTP::Head.new(uri)
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
@mehdi-farsi
mehdi-farsi / _links.html.erb
Created December 3, 2014 10:26
Sort by ActiveRecord column
@mehdi-farsi
mehdi-farsi / db.rb
Last active February 25, 2016 15:44
Select a specific attribute of the first and last record in an ActiveRecord Model
# Example:
#
# A Product Model with a :name attribute
first, last = Product.pluck(:name).values_at(0, Product.count - 1)
@mehdi-farsi
mehdi-farsi / GET.txt
Created December 7, 2014 18:25
Pass an array as parameter to an HTTP request
# Example:
#
# Wanna pass an array of categories via a GET request.
http://YOUR_URL/?category[]=handbag&category[]=wallet
@mehdi-farsi
mehdi-farsi / proc_and_lambda.rb
Created December 9, 2014 16:53
Difference between Proc and Lambda
# Procs and Lambdas. They are both a Proc object.
#
# The main differences:
#
# 1- Lamdbas are strict about argument number instead of Procs.
# 2- Lambda :return returns out the scope. Proc :return returns out of the calling scope.
# Lamdbas are strict about argument number instead of Procs.
l = lambda { |word| puts word }