mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-26 23:23:55 +00:00
[ruby/ru] [ruby/en] added notes about postfix-if and about
This commit is contained in:
parent
ffb9c5f909
commit
4a14d54eb5
@ -231,6 +231,7 @@ new_hash.value?(3) #=> true
|
||||
|
||||
# Управление ходом выполнения (Управляющие структуры)
|
||||
|
||||
# Условия
|
||||
if true
|
||||
'Если истина'
|
||||
elsif false
|
||||
@ -239,6 +240,15 @@ else
|
||||
'Во всех других случаях (тоже опционально)'
|
||||
end
|
||||
|
||||
# Если условие контролирует выполнение не блока кода, а единственного выражения,
|
||||
# можно использовать постфиксную запись условного оператора
|
||||
warnings = ['Отсутствует отчество', 'Слишком короткий адрес']
|
||||
puts("Обратите внимание:\n" + warnings.join("\n")) if !warnings.empty?
|
||||
|
||||
# Иногда условие лучше звучит с `unless`, чем с `if`
|
||||
puts("Обратите внимание:\n" + warnings.join("\n")) unless warnings.empty?
|
||||
|
||||
# Циклы
|
||||
for counter in 1..5
|
||||
puts "итерация #{counter}"
|
||||
end
|
||||
|
@ -247,6 +247,14 @@ else
|
||||
'else, also optional'
|
||||
end
|
||||
|
||||
# If a condition controls invokation of a single statement rather than a block of code
|
||||
# you can use postfix-if notation
|
||||
warnings = ['Patronimic is missing', 'Address too short']
|
||||
puts("Some warnings occurred:\n" + warnings.join("\n")) if !warnings.empty?
|
||||
|
||||
# Rephrase condition if `unless` sounds better than `if`
|
||||
puts("Some warnings occurred:\n" + warnings.join("\n")) unless warnings.empty?
|
||||
|
||||
# Loops
|
||||
# In Ruby, traditional `for` loops aren't very common. Instead, these
|
||||
# basic loops are implemented using enumerable, which hinges on `each`.
|
||||
|
Loading…
Reference in New Issue
Block a user