【发布时间】:2015-11-11 18:52:05
【问题描述】:
我编写了一个程序,它接收每天的股票价格数组,然后返回应该买入和卖出股票的日期。我有一个全局变量$negatives 显示买卖日。我想将此全局变量作为 puts 语句的一部分返回。但是,目前,什么都没有出现。我没有看到我的 puts 声明。知道发生了什么吗?
def stock_prices array
$largest_difference = 0
array.each_with_index {|value, index|
if index == array.size - 1
exit
end
array.each {|i|
$difference = value - i
if ($difference <= $largest_difference) && (index < array.rindex(i))
$negatives = [index, array.rindex(i)]
$largest_difference = $difference
end
}
}
puts "The stock should be bought and sold at #{$negatives}, respectively"
end
puts stock_prices([10,12,5,3,20,1,9,20])
【问题讨论】:
标签: ruby global-variables puts