Created
December 16, 2018 21:16
-
-
Save shamritskiy3468/df373252fc66c5d113951b4e7a6cdafd to your computer and use it in GitHub Desktop.
task3
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
def make_output(array) | |
40.times { print "-"} | |
puts "\n#{array.join(", ")}" | |
end | |
def generate_array | |
array = [] | |
rand(10..20).times { array << rand(-10..10)} | |
array | |
end | |
#it can be made easier but I don't understand.. | |
#when i tried to make: array.map { |i| i = array.min if i.positive? } it makes all positive numbers in nil, why? | |
def rework_array(array, min) | |
array.each_with_index do |item, index| | |
if array[index] > 0 | |
array[index] = min | |
end | |
end | |
end | |
array_start = generate_array | |
min = array_start.min | |
puts "Minimal element = #{min}" | |
make_output(rework_array(array_start, min)) |
And you don't need: a = something, because value returned from the block will be given as the element of new array
And you've got nil not instead positive but instead negative numbers ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#when i tried to make: array.map { |i| i = array.min if i.positive? } it makes all positive numbers in nil, why?
This is because Ruby is a language of expressions ;)
Ruby calculates every expression.
What do you think about the result of calculation in this case: array.min if i.positive? when I.positive? is TRUE an FALSE?