Ruby Gotchas
-
Upload
nelson-senna-do-amaral -
Category
Technology
-
view
86 -
download
0
Transcript of Ruby Gotchas
![Page 1: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/1.jpg)
RUBY GOTCHASAlgumas surpresas que esperam os novatos
![Page 2: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/2.jpg)
Sobre mim
• Programador
• Trabalho no Glio
• Novato em Ruby
• @nelson_senna no Twitter
![Page 3: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/3.jpg)
Motivação
![Page 4: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/4.jpg)
– Yukihiro “Matz” Matsumoto
“Ruby is simple in appearance, but is very complex inside, just like our human body.”
![Page 5: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/5.jpg)
Ruby pode ser surpreendente!
![Page 6: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/6.jpg)
Mágica!
![Page 7: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/7.jpg)
No fim tudo é um objeto
![Page 8: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/8.jpg)
Gotcha #1
O que é verdade?
![Page 9: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/9.jpg)
2.3.0 :001 > 0 ? true : false
![Page 10: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/10.jpg)
2.3.0 :001 > 0 ? true : false => true
![Page 11: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/11.jpg)
2.3.0 :001 > ‘’ ? true : false
![Page 12: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/12.jpg)
2.3.0 :001 > ‘’ ? true : false => true
![Page 13: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/13.jpg)
2.3.0 :001 > [] ? true : false
![Page 14: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/14.jpg)
2.3.0 :001 > [] ? true : false => true
![Page 15: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/15.jpg)
2.3.0 :001 > nil ? true : false
![Page 16: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/16.jpg)
2.3.0 :001 > nil ? true : false => false
![Page 17: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/17.jpg)
2.3.0 :001 > false ? true : false
![Page 18: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/18.jpg)
2.3.0 :001 > false ? true : false => false
![Page 19: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/19.jpg)
A pegadinha
Em Ruby só nil e false são considerados “falsey” qualquer outro valor é considerado “truthy”.
![Page 20: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/20.jpg)
Isso é útil?
address = ‘Rua Casa do Ator, 275’ address2 = nil # Famoso complemento
full_address = “Endereço: #{address}\n #{“Complemento: #{address2}” if address2}”
![Page 21: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/21.jpg)
Gotcha #2
(&& ou and) e (|| ou or)?
![Page 22: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/22.jpg)
song = { name: 'Nelson', duration: 300 }
duration = song[:duration]
minutes = duration && duration / 60
puts minutes # imprime 5
Duração em minutos
![Page 23: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/23.jpg)
song = { name: 'Nelson', duration: 300 }
duration = song[:duration]
minutes = duration and duration / 60
puts minutes # imprime 300
Duração em minutos II
![Page 24: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/24.jpg)
O operador = tem maior precedência que o
operador and
![Page 25: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/25.jpg)
O que o Ruby vê
duration = song[:duration]
(minutes = duration) and (duration / 60)
![Page 26: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/26.jpg)
Evitando surpresas
Não use and e or!
![Page 27: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/27.jpg)
Gotcha #3
Constantes podem não ser constantes
![Page 28: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/28.jpg)
Definindo constantes
Ruby considerada constantes “nomes” que começam com letra maiúscula.
![Page 29: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/29.jpg)
Logo
2.3.0 :001 > defined? String => “constant”
![Page 30: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/30.jpg)
Mas…
2.3.0 :001 > String = ‘Nelson’ (irb):2: warning: already initialized constant String (irb):1: warning: previous definition of String was here => “Nelson” 2.3.0 :001 > puts String Nelson => nil
![Page 31: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/31.jpg)
A pegadinha
Em Ruby constantes são referências para objetos e por isso podem ser alteradas.
![Page 32: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/32.jpg)
Evitando surpresas
module CardTypes VISA = ‘visa’.freeze MASTERCARD = ‘mastercard’.freeze end
CardTypes.freeze
![Page 33: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/33.jpg)
Evitando surpresas
# Para arrays precisamos dar freeze # em todos os elementos do mesmo
[‘visa’, ‘mastercard’].map(&:freeze).freeze!
![Page 34: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/34.jpg)
Gotcha #4
Blocks, procs e lambdas
![Page 35: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/35.jpg)
Blocks
10.times { puts ‘A lot of strings’ }
10.times do |number| puts “My number is #{number}” end
![Page 36: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/36.jpg)
Blocksdef get_request(url) uri = URI(url)
response = Net::HTTP.get_response(uri) return response unless block_given?
yield(response) end
get_request(url) do |response| puts response.body end
![Page 37: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/37.jpg)
Blocks
![Page 38: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/38.jpg)
Procs
proc { puts ‘Hello world’ }
Proc.new { puts ‘Hello world’ }
![Page 39: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/39.jpg)
Procs
p = Proc.new do |name| puts “Hello #{name}” end
p.call # imprime “Hello “
![Page 40: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/40.jpg)
A pegadinhadef my_method proc = Proc.new { return “from proc” }
return “from method” end
puts my_method # imprime “from proc”
![Page 41: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/41.jpg)
Lambdas
-> { puts ‘Hello world’ }
lambda { puts ‘Hello world’ }
![Page 42: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/42.jpg)
Lambdas
l = lambda do |name| puts “Hello #{name}” end
l.call # Erro!
![Page 43: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/43.jpg)
A pegadinhadef my_method lam = lambda { return “from proc” }
return “from method” end
puts my_method # imprime “from method”
![Page 44: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/44.jpg)
Resumão!
![Page 45: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/45.jpg)
Extras
![Page 46: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/46.jpg)
Dúvidas?
![Page 47: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/47.jpg)
Referências• http://www.tutorialspoint.com/ruby/ruby_variables.htm
• http://awaxman11.github.io/blog/2013/08/05/what-is-the-difference-between-a-block/
• https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/18-blocks/lessons/64-blocks-procs-lambdas
• http://www.eriktrautman.com/posts/ruby-explained-blocks-procs-and-lambdas-aka-closures
• https://blog.newrelic.com/2015/04/30/weird-ruby-part-4-code-pods/
• http://blog.jayfields.com/2006/12/ruby-constant-values.html
• http://www.informit.com/articles/article.aspx?p=2251208&seqNum=4
• http://rubylearning.com/blog/2010/09/27/almost-everything-is-an-object-and-everything-is-almost-an-object/
![Page 48: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/48.jpg)
Referências• blog.elpassion.com/ruby-gotchas/
• http://phrogz.net/programmingruby/tut_expressions.html
• https://github.com/bbatsov/ruby-style-guide
• https://www.ruby-lang.org/en/documentation/ruby-from-other-languages/
• http://www.tutorialspoint.com/ruby/ruby_operators.htm
• https://blog.engineyard.com/2009/3-ruby-quirks-you-have-to-love
• http://stackoverflow.com/questions/372652/what-are-the-ruby-gotchas-a-newbie-should-be-warned-about
![Page 49: Ruby Gotchas](https://reader031.fdocuments.in/reader031/viewer/2022030305/58731f031a28ab673e8b7073/html5/thumbnails/49.jpg)
Obrigado