The document discusses Ruby logic and control flow. It explains logic operators like !, &&, || and control flow structures like if, unless. It provides examples of using these operators and structures to control program flow based on conditional logic. It also emphasizes always using clean and consistent code style according to common Ruby style guides.
8. Let`s see
foo = 42 && foo / 2 foo = 42 and foo / 2
NoMethodError: unde鍖ned method
`/' for nil:NilClass 21
9. 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
10. When we are lazy
job = that_is_the_question?
job or not_to_do
# => Noting to do
job || not_to_do
# => Noting to do
11. 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?
12. The solution for control 鍖ow with ||
is to place action in parentheses
job || (puts Noting to do)
13. 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 鍖x this
14. Control 鍖ow practice
if job
to_do
end
unless job
not_to_do
end
to_do if job not_to_do unless job
15. Control 鍖ow 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
16. Control 鍖ow 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 鍖ow with logic operators
17. 1 || 2 && nil
1 or 2 and nil
Some kind of magic
18. 1 || 2 && nil # => 1
1 or 2 and nil # => nil
Some kind of magic
20. 1 || 2 && nil # => 1
1 or 2 and nil # => nil
Some kind of magic
Higher priority
Lower priority