Gotchas and Stack Traces in Ruby
-
Upload
ombu-labs-the-lean-software-boutique -
Category
Technology
-
view
222 -
download
0
Transcript of Gotchas and Stack Traces in Ruby
![Page 1: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/1.jpg)
Gotchas and Stack Traces in
RubyOmbuLabs, September 2015
![Page 2: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/2.jpg)
#1: Whitespacedef method(arg1, arg2)end
method 1, 2 # OKmethod(1, 2) # OKmethod (1, 2) # Error!
![Page 3: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/3.jpg)
SyntaxError: (irb):24: syntax error, unexpected ',', expecting ‘)'from (irb):7:in `method'
![Page 4: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/4.jpg)
#1: Whitespacedef method 42end
num = 21method/num # OKmethod / num # OKmethod/ num # OKmethod /num # Error!
![Page 5: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/5.jpg)
ArgumentError: wrong number of arguments (1 for 0)
from (irb):7:in `method'
![Page 6: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/6.jpg)
#2: True & False[].any? => false[1].any? => true[:foo, :bar].any => true
[false].any? => false[nil].any? => false[false, nil].any? => false[false, true].any? => true
![Page 7: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/7.jpg)
`any?` is actually “Are any elements truthy?”
![Page 8: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/8.jpg)
#3: Constants (not)FOO = 5 => 5 FOO = 7(irb):3: warning: already initialized constant FOO => 7 FOO => 7
![Page 9: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/9.jpg)
Constants can be reassigned, Ruby will just throw a warning and let you do it anyway.
![Page 10: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/10.jpg)
def foo 0/0end
def bar fooend
def baz barend
baz
![Page 11: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/11.jpg)
➜ ombushop git:(develop) ✗ ruby test.rbtest.rb:2:in `/': divided by 0 (ZeroDivisionError)
from test.rb:2:in `foo'from test.rb:6:in `bar'from test.rb:10:in `baz'from test.rb:13:in `<main>'
![Page 12: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/12.jpg)
def foo my_correctly_spelled_variable = "hey!" puts my_incorrectly_spelled_variableend
def bar fooend
def baz barend
baz
![Page 13: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/13.jpg)
➜ ombushop git:(develop) ✗ ruby test.rbtest.rb:3:in `foo': undefined local variable or method `my_incorrectly_spelled_variable' for main:Object (NameError)
from test.rb:7:in `bar'from test.rb:11:in `baz'from test.rb:14:in `<main>'
![Page 14: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/14.jpg)
def foo my_correctly_spelled_variable = "hey!" puts my_incorrectly_spelled_variableend
def bar my_incorrectly_spelled_variable = "hey!" fooend
def baz my_incorrectly_spelled_variable = "hey!" barend
baz
![Page 15: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/15.jpg)
undefined local variable or method `my_incorrectly_spelled_variable' for main:Object (NameError)
![Page 16: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/16.jpg)
➜ ombushop git:(master) ✗ ruby test.rbtest.rb:3:in `foo': undefined local variable or method `my_incorrectly_spelled_variable' for main:Object (NameError)
from test.rb:8:in `bar'from test.rb:13:in `baz'from test.rb:16:in `<main>'
![Page 17: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/17.jpg)
![Page 18: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/18.jpg)
![Page 19: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/19.jpg)
![Page 20: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/20.jpg)
TL;DR: Always paste the full stack trace of the error you come across when asking for help.
![Page 21: Gotchas and Stack Traces in Ruby](https://reader035.fdocuments.in/reader035/viewer/2022070513/58827fe01a28ab24788b63bd/html5/thumbnails/21.jpg)
THANK YOU!questions?