这是您的代码清理了一下:
require 'csv'
puts "This program has all the General Electric stock information from November
27, 1960 to October 8 2013. Please enter the date you would like to find the
stock information of like this: 1997-10-30 (year-month-day)."
date = gets.chomp
puts "Please enter what information about the stock you would like to know:
open, high, low, close, volume, changed, percent change,adjusted closing, trade
value, or trade volume. Please put a underscore in place of all spaces."
input = gets.chomp
CSV.foreach(
'test.csv',
:headers => true,
:return_headers => false
) do |row|
next unless row['date'] == date
if input == 'open'
puts "The open of your stock is: #{ row['open'] }"
elsif input == 'high'
puts "The high of your stock is: #{ row['high'] }"
elsif input == 'low'
puts "The low of your stock is: #{ row['low'] }"
elsif input == 'close'
puts "The close of your stock is: #{ row['close'] }"
elsif input == 'volume'
puts "The volume of your stock is: #{ row['volume'] }"
elsif input == 'changed'
puts "The volume of your stock is: #{ row['changed'] }"
elsif input == 'percent_change'
puts "The percent change of your stock is: #{ row['percent_change'] }"
elsif input == 'adjusted_closing'
puts "The open adjusted closing of your stock is: #{ row['adjusted_closing'] }"
elsif input == 'trade_value'
puts "The trade value of your stock is: #{ row['trade_value'] }"
elseif input == 'trade_volume'
puts "The trade volume of your stock is: #{ row['trade_volume'] }"
else
puts "An unknown option was entered."
end
end
使用这些输入:
1997-10-30
open
回报:
The open of your stock is: 23.84
The open of your stock is: 24.18
The open of your stock is: 24.22
使用 Ruby 的 CSV 类。它已经编写好了,并且已经过调试和测试,因此您不必重新发明那个轮子。您可以告诉它使用 CSV 文件的第一行作为标题,以及是否应该返回这些标题。此外,它可以将一行数据作为数组或散列返回。将其作为哈希返回对于您想要做的事情非常有用。
以下是一些重构,向您展示如何简化和删除冗余代码:
require 'csv'
puts "This program has all the General Electric stock information from November
27, 1960 to October 8 2013. Please enter the date you would like to find the
stock information of like this: 1997-10-30 (year-month-day)."
date = gets.chomp
puts "Please enter what information about the stock you would like to know:
open, high, low, close, volume, changed, percent change,adjusted closing, trade
value, or trade volume. Please put a underscore in place of all spaces."
input = gets.chomp
CSV.foreach(
'test.csv',
:headers => true,
:return_headers => false
) do |row|
next unless row['date'] == date
case input
when 'open'
puts "The open of your stock is: #{ row['open'] }"
when 'high'
puts "The high of your stock is: #{ row['high'] }"
when 'low'
puts "The low of your stock is: #{ row['low'] }"
when 'close'
puts "The close of your stock is: #{ row['close'] }"
when 'volume'
puts "The volume of your stock is: #{ row['volume'] }"
when 'changed'
puts "The volume of your stock is: #{ row['changed'] }"
when 'percent_change'
puts "The percent change of your stock is: #{ row['percent_change'] }"
when 'adjusted_closing'
puts "The open adjusted closing of your stock is: #{ row['adjusted_closing'] }"
when 'trade_value'
puts "The trade value of your stock is: #{ row['trade_value'] }"
when 'trade_volume'
puts "The trade volume of your stock is: #{ row['trade_volume'] }"
else
puts "An unknown option was entered."
end
end
此版本替换了if/elseif/else
有助于case/when
简化逻辑的 ,并为相同的输入返回相同的内容。
require 'csv'
puts "This program has all the General Electric stock information from November
27, 1960 to October 8 2013. Please enter the date you would like to find the
stock information of like this: 1997-10-30 (year-month-day)."
date = gets.chomp
puts "Please enter what information about the stock you would like to know:
open, high, low, close, volume, changed, percent change,adjusted closing, trade
value, or trade volume. Please put a underscore in place of all spaces."
input = gets.chomp
CSV.foreach(
'test.csv',
:headers => true,
:return_headers => false
) do |row|
next unless row['date'] == date
val = case input
when 'open'
'open'
when 'high'
'high'
when 'low'
'low'
when 'close'
'close'
when 'volume'
'volume'
when 'changed'
'changed'
when 'percent_change'
'percent_change'
when 'adjusted_closing'
'adjusted_closing'
when 'trade_value'
'trade_value'
when 'trade_volume'
'trade_volume'
else
puts "An unknown option was entered."
next
end
puts "The %s of your stock is: %s" % [val.gsub('_', ' '), row[val] ]
end
这建立在case
声明的基础上,只返回更改的内容,但也向我们展示了大量的冗余。
require 'csv'
puts "This program has all the General Electric stock information from November
27, 1960 to October 8 2013. Please enter the date you would like to find the
stock information of like this: 1997-10-30 (year-month-day)."
date = gets.chomp
puts "Please enter what information about the stock you would like to know:
open, high, low, close, volume, changed, percent change, adjusted closing,
trade value, or trade volume. Please put a underscore in place of all spaces."
input = gets.chomp
CSV.foreach(
'test.csv',
:headers => true,
:return_headers => false
) do |row|
puts "The %s of your stock is: %s" % [input.gsub('_', ' '), row[input] ] if (row['date'] == date)
end
去除冗余导致此代码。