使用Ruby的Code Block创建一个Hash比较函数
在使用Ruby开发时,经常会遇到需要比较两个Hash对象的值的场合。代码类似如下:
x = Hash.newx[:a] = 'a'y = Hash.newy[:a] = 'b'diff(x, y) do |key|puts "find difference for key #{key}: x = #{x[key]}, y = #{y[key]}"end
发布时间: 2012-06-28 15:20:03 作者: rapoo
使用Ruby的Code Block创建一个Hash比较函数
在使用Ruby开发时,经常会遇到需要比较两个Hash对象的值的场合。代码类似如下:
x = Hash.newx[:a] = 'a'y = Hash.newy[:a] = 'b'diff(x, y) do |key|puts "find difference for key #{key}: x = #{x[key]}, y = #{y[key]}"end