Created
September 28, 2013 20:55
-
-
Save makaroni4/6746445 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train_path.each do |obj| | |
result << obj.obj_id | |
end if train_path.any? | |
# | |
# 1. Если ты вызываешь метод .each, то как минимум переменная | |
# должна иметь множественное число, это ведь коллекция (массив), | |
# т.е. это train_paths | |
# | |
# 2. Если массив пуст, то метод .each сразу вернет пустой массив | |
# и if не нужно вызывать, чтобы узнать пустой ли массив | |
# | |
# train_paths = [] | |
# train_paths.each do |train| | |
# raise Exception | |
# end | |
# | |
# => [] | |
# | |
# 3. Чтобы собрать какой-то параметр у коллекции объектов, можно юзать inject. | |
# Тогда не нужно объявлять временную переменную: | |
# | |
# [1,2,3].inject(0) { |n, sum| sum += n } | |
# => 6 | |
# | |
train_paths.inject do |train, ids| | |
ids += train.obj_id | |
ids | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment