This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]+/] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Flat the array layer by layer | |
# | |
# Insights: | |
# | |
# - Using recursion. | |
# | |
class Array | |
def my_flatten(depth = nil) | |
if depth.nil? || depth.is_a?(Fixnum) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= link_to "Name", sort: "Name" %> | |
<%= link_to "Price", sort: "price" %> | |
<%= link_to "Popular", sort: "popular" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example: | |
# | |
# A Product Model with a :name attribute | |
first, last = Product.pluck(:name).values_at(0, Product.count - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example: | |
# | |
# Wanna pass an array of categories via a GET request. | |
http://YOUR_URL/?category[]=handbag&category[]=wallet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 } |