Created
February 21, 2019 13:17
-
-
Save serradura/1d400ac7d3887c0fe3a3fa3639aa5f5a to your computer and use it in GitHub Desktop.
TIS = Today I shared
This file contains hidden or 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
# | |
# Métodos globais | |
# | |
def calc_sum(a, b) | |
a + b | |
end | |
puts calc_sum(1, 3) | |
# Classes com métodos estáticos | |
class Calc | |
def self.sum(a, b) | |
a + b | |
end | |
end | |
puts Calc.sum(1, 1) | |
# | |
# Classes com métodos de instância | |
# | |
class Calc | |
def sum(a, b) | |
a + b | |
end | |
end | |
calc = Calc.new | |
puts calc.sum(1, 1) | |
# | |
# Funções (Ruby lambdas) | |
# | |
calc_sum = -> (a, b) { a + b } | |
puts calc_sum.call(1, 2) | |
puts calc_sum.(1, 2) | |
puts calc_sum[1, 2] | |
=begin | |
// Equivalência da função acima em Ruby escrita em JS; | |
// ES5 | |
calcSum = function(a, b) { | |
return a + b; | |
} | |
// ES6 | |
calcSum = (a, b) => a + b; | |
=end |
This file contains hidden or 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 'benchmark' | |
class User | |
def self.find(id) | |
sleep(0.5) | |
puts "Finding user by id: #{id}..." | |
id | |
end | |
end | |
def current_user | |
@current_user = User.find(1) | |
end | |
puts Benchmark.measure { 5.times { puts current_user } } | |
# Finding user by id: 1... | |
# Finding user by id: 1... | |
# Finding user by id: 1... | |
# Finding user by id: 1... | |
# Finding user by id: 1... | |
# 0.000000 0.000000 0.000000 ( 2.500786) | |
def current_user | |
@current_user ||= User.find(1) | |
end | |
puts Benchmark.measure { 5.times { puts current_user } } | |
# Finding user by id: 1... | |
# 0.000000 0.000000 0.000000 ( 0.500157) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment