删除Ruby哈希值中的前导空格
我正在处理Chris Pine的“学习编程”一书中的一个示例问题,在删除哈希值中的空白时遇到了一个问题 我从一个包含姓名和生日信息的txt文件开始,如下所示:删除Ruby哈希值中的前导空格,ruby,hash,strip,Ruby,Hash,Strip,我正在处理Chris Pine的“学习编程”一书中的一个示例问题,在删除哈希值中的空白时遇到了一个问题 我从一个包含姓名和生日信息的txt文件开始,如下所示: Christopher Alexander, Oct 4, 1936 Christopher Lambert, Mar 29, 1957 Christopher Lee, May 27, 1922 Christopher Lloyd, Oct 22, 1938 Christopher Pine,
Christopher Alexander, Oct 4, 1936
Christopher Lambert, Mar 29, 1957
Christopher Lee, May 27, 1922
Christopher Lloyd, Oct 22, 1938
Christopher Pine, Aug 3, 1976
然后我遍历每一行,在第一个逗号处拆分,然后尝试遍历每个键、值以去除空白
birth_dates = Hash.new {}
File.open 'birthdays.txt', 'r' do |f|
f.read.each_line do |line|
name, date = line.split(/,/, 2)
birth_dates[name] = date
birth_dates.each_key { |a| birth_dates[a].strip! }
end
但没有任何东西被剥夺
{"Christopher Alexander"=>" Oct 4, 1936", "Christopher Lambert"=>" Mar 29, 1957", "Christopher Lee"=>" May 27, 1922", "Christopher Lloyd"=>" Oct 22, 1938", "Christopher Pine"=>" Aug 3, 1976", "Christopher Plummer"=>" Dec 13, 1927", "Christopher Walken"=>" Mar 31, 1943", "The King of Spain"=>" Jan 5, 1938"}
我见过一些使用.map的数组解决方案,但这是我遇到的唯一一个哈希示例。你知道为什么它对我不起作用吗
更新:根据sawa的评论删除多余的咀嚼 我会写这个
File.open 'birthdays.txt', 'r' do |f|
f.read.each_line do |line|
name, date = line.split(/,/, 2)
birth_dates[name] = date.chomp
birth_dates.each_key { |a| birth_dates[a].strip! }
end
详情如下:
File.open 'birthdays.txt', 'r' do |f|
f.read.each_line do |line|
name, date = line.split(/,/, 2)
birth_dates[name] = date.chomp.strip
end
end
或
为了解析逗号分隔的文件,我使用如下CSV
def parse_birthdays(file='birthdays.txt', hash={})
CSV.foreach(file, :converters=> lambda {|f| f ? f.strip : nil}){|name, date, year|hash[name] = "#{year}-#{date.gsub(/ +/,'-')}" }
hash
end
parse_birthdays
# {"Christopher Alexander"=>"1936-Oct-4", "Christopher Lambert"=>"1957-Mar-29", "Christopher Lee"=>"1922-May-27", "Christopher Lloyd"=>"1938-Oct-22", "Christopher Pine"=>"1976-Aug-3"}
当然,如果你需要真正的约会,你可以放弃lambda
def parse_birthdays(file='birthdays.txt', hash={})
CSV.foreach(file){|name, date, year|hash[name] = Date.parse("#{year}-#{date}")}
hash
end
parse_birthdays
# {"Christopher Alexander"=>#<Date: 2014-10-04 ((2456935j,0s,0n),+0s,2299161j)>, "Christopher Lambert"=>#<Date: 2014-03-29 ((2456746j,0s,0n),+0s,2299161j)>, "Christopher Lee"=>#<Date: 2014-05-27 ((2456805j,0s,0n),+0s,2299161j)>, "Christopher Lloyd"=>#<Date: 2014-10-22 ((2456953j,0s,0n),+0s,2299161j)>, "Christopher Pine"=>#<Date: 2014-08-03 ((2456873j,0s,0n),+0s,2299161j)>}
def parse_birthdays(file='birthdays.txt',hash={})
CSV.foreach(文件){name,date,year{hash[name]=date.parse(“{year}-{date}”)}
搞砸
结束
你的生日
#{“克里斯托弗·亚历山大”,“克里斯托弗·兰伯特”,“克里斯托弗·李”,“克里斯托弗·劳埃德”,“克里斯托弗·派恩”
您的问题无法重现。strip
之前执行chomp
是多余的<代码>条带就足够了。谢谢@sawa-I更新了示例以消除冗余。尽管如此,在我的示例和下面的两个答案中,terminal仍然显示在月前还有一些空间。这只是终端中的一个显示问题吗?如果线路被返回而中断,那是CSV吗?运行第一个选项,它很好地分离了事物,但是它看起来像是月前的前导空间,那是因为这些空间在一个字符串的中间,那么你需要一个规则的ExpRESIVE,我修改了我的答案的第一部分,使用a-进行替换,但也可以使用单个空间,但运行良好。
def parse_birthdays(file='birthdays.txt', hash={})
CSV.foreach(file){|name, date, year|hash[name] = Date.parse("#{year}-#{date}")}
hash
end
parse_birthdays
# {"Christopher Alexander"=>#<Date: 2014-10-04 ((2456935j,0s,0n),+0s,2299161j)>, "Christopher Lambert"=>#<Date: 2014-03-29 ((2456746j,0s,0n),+0s,2299161j)>, "Christopher Lee"=>#<Date: 2014-05-27 ((2456805j,0s,0n),+0s,2299161j)>, "Christopher Lloyd"=>#<Date: 2014-10-22 ((2456953j,0s,0n),+0s,2299161j)>, "Christopher Pine"=>#<Date: 2014-08-03 ((2456873j,0s,0n),+0s,2299161j)>}