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
# | |
# 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: | |
# | |
# 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 } |
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 Category < ActiveRecord::Base | |
has_and_belongs_to_many :products | |
end | |
class Product < ActiveRecord::Base | |
has_and_belongs_to_many :categories | |
# :c can be an array of categories. | |
scope :by_categories, ->(c) { includes(:categories).where(categories: {id: c}) } | |
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
# Example: | |
# | |
# 1 | |
# / \ | |
# 2 12 | |
# / | |
# 4 | |
class Node | |
attr_accessor :left, :right, :value |
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 Hello | |
def self.world | |
puts 'Hello World !' | |
end | |
end | |
def return_hello | |
return Hello | |
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
"olo".gsub(/[ol]/, 'o' => 'l', 'l' => 'o') | |
# output | |
# | |
# 'lol' |