Skip to content

Instantly share code, notes, and snippets.

@shamritskiy3468
Created December 16, 2018 21:16
Show Gist options
  • Save shamritskiy3468/df373252fc66c5d113951b4e7a6cdafd to your computer and use it in GitHub Desktop.
Save shamritskiy3468/df373252fc66c5d113951b4e7a6cdafd to your computer and use it in GitHub Desktop.
task3
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))
@aya-soft
Copy link

aya-soft commented Dec 20, 2018

#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?

@aya-soft
Copy link

And you don't need: a = something, because value returned from the block will be given as the element of new array

@aya-soft
Copy link

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