gotchas and stack traces in ruby

21
Gotchas and Stack Traces in Ruby OmbuLabs, September 2015

Upload: ombu-labs-the-lean-software-boutique

Post on 21-Jan-2017

224 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Gotchas and Stack Traces in Ruby

Gotchas and Stack Traces in

RubyOmbuLabs, September 2015

Page 2: Gotchas and Stack Traces in Ruby

#1: Whitespacedef method(arg1, arg2)end

method 1, 2 # OKmethod(1, 2) # OKmethod (1, 2) # Error!

Page 3: Gotchas and Stack Traces in Ruby

SyntaxError: (irb):24: syntax error, unexpected ',', expecting ‘)'from (irb):7:in `method'

Page 4: Gotchas and Stack Traces in Ruby

#1: Whitespacedef method 42end

num = 21method/num # OKmethod / num # OKmethod/ num # OKmethod /num # Error!

Page 5: Gotchas and Stack Traces in Ruby

ArgumentError: wrong number of arguments (1 for 0)

from (irb):7:in `method'

Page 6: Gotchas and Stack Traces in Ruby

#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

`any?` is actually “Are any elements truthy?”

Page 8: Gotchas and Stack Traces in Ruby

#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

Constants can be reassigned, Ruby will just throw a warning and let you do it anyway.

Page 10: Gotchas and Stack Traces in Ruby

def foo 0/0end

def bar fooend

def baz barend

baz

Page 11: Gotchas and Stack Traces in Ruby

➜ 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

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

➜ 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

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

undefined local variable or method `my_incorrectly_spelled_variable' for main:Object (NameError)

Page 16: Gotchas and Stack Traces in Ruby

➜ 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
Page 18: Gotchas and Stack Traces in Ruby
Page 19: Gotchas and Stack Traces in Ruby
Page 20: Gotchas and Stack Traces in Ruby

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

THANK YOU!questions?