Slide 1

Slide 1 text

To_do or not_to_do that_is_the_question? Ruby logic control flow guide

Slide 2

Slide 2 text

What we know about logic in ruby?

Slide 3

Slide 3 text

Logic operators: (by priority) ! && || not and, or

Slide 4

Slide 4 text

What we know about control flow in ruby?

Slide 5

Slide 5 text

Control flow: ?: if unless

Slide 6

Slide 6 text

Hmm. Interesting, how we can to relate this parts?

Slide 7

Slide 7 text

Let`s see foo = 42 && foo / 2 foo = 42 and foo / 2

Slide 8

Slide 8 text

Let`s see foo = 42 && foo / 2 foo = 42 and foo / 2 NoMethodError: undefined method `/' for nil:NilClass 21

Slide 9

Slide 9 text

Help methods def to_do puts ‘What to do?’ end def not_to_do puts ‘Nothing to do’ end def that_is_the_question?(question = false) question end

Slide 10

Slide 10 text

When we are lazy job = that_is_the_question? job or not_to_do # => ‘Noting to do’ job || not_to_do # => ‘Noting to do’

Slide 11

Slide 11 text

When we are lazy job or not_to_do # => ‘Noting to do’ job || not_to_do # => ‘Noting to do’ job or puts ‘Noting to do’ # => ‘Noting to do’ job || puts ‘Noting to do’ # => syntax error, unexpected tSTRING_BEG, expecting kDO or '{' or '(' job || puts 'Noting to do' job = that_is_the_question?

Slide 12

Slide 12 text

The solution for control flow with ‘||’ is to place action in parentheses job || (puts ‘Noting to do’)

Slide 13

Slide 13 text

The same situation when we a hard-working job = that_is_the_question? ‘of course’ job and to_do # => ‘What to do?’ job && to_do # => ‘What to do?’ job and puts ‘What to do?’ # => ‘What to do? job && puts ‘Noting to do’ # => syntax error, unexpected tSTRING_BEG, expecting kDO or '{' or '(' job || puts 'Noting to do' But now we know, how to fix this

Slide 14

Slide 14 text

Control flow practice if job to_do end unless job not_to_do end to_do if job not_to_do unless job

Slide 15

Slide 15 text

Control flow practice if job to_do end unless job not_to_do end to_do if job not_to_do unless job job and to_do job or not_to_do job or raise ‘LAZY boy’

Slide 16

Slide 16 text

Control flow practice if job to_do end unless job not_to_do end to_do if job not_to_do unless job job and to_do job or not_to_do job or raise ‘LAZY boy’ Control flow with logic operators

Slide 17

Slide 17 text

1 || 2 && nil 1 or 2 and nil Some kind of magic

Slide 18

Slide 18 text

1 || 2 && nil # => 1 1 or 2 and nil # => nil Some kind of magic

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

1 || 2 && nil # => 1 1 or 2 and nil # => nil Some kind of magic Higher priority Lower priority

Slide 21

Slide 21 text

They are the same, but so different

Slide 22

Slide 22 text

What we have? use ‘&&’ and ‘||’ only for logic flow use ‘and’ and ‘or’ only for control flow Golden rule:

Slide 23

Slide 23 text

And something else Always use regimented code style https:/ /github.com/styleguide/ruby https:/ /github.com/bbatsov/ruby-style-guide

Slide 24

Slide 24 text

Ruby rocks!

Slide 25

Slide 25 text

Thanks for your attention