After reading the "Refactoring for PL/SQL Developers" in the January
issue of the Oracle magazine, I tried to implement a program that
compares two files for equality. I came up with the following trivial
solution
After reading the "Refactoring for PL/SQL Developers" in the January
issue of the Oracle magazine, I tried to implement a program that
compares two files for equality. I came up with the following trivial
solution
After reading "Refactoring for PL/SQL Developers" in January's Oracle
magazine, I tried to implement a program that compares two files for
equality. I came up with the following trivial solution:
puts (IO.readlines file[0]) == (IO.readlines file[1])
Have you other suggestions?
I came up with this:
class File
SIZE =1024
include Comparable
def <=> f # Does not rewind, before or after.
s1 =s2 =s =n =0 # Efficiency.
begin # At least once.
s1 ,s2 =[ self ,f].collect do |a|
s =a.read( SIZE)
s ? s :String.new # Or ''.
end
end while 0 ==( n =(s1 <=>s2)) &&
SIZE ==s1.length
n # Return.
end
end # class File
Kind regards,
···
Edgardo Hames <ehames@gmail.com> Jan 12, 2005 at 01:26 PM wrote:
Another alternative - probably slow as h*ll, but...
def files_equals?(*files)
require 'digest/md5'
return files.map do |file|
Digest::MD5.hexdigest(File.read(file))
end.uniq.size == 1
end
//Anders
···
On Wed, Jan 12, 2005 at 09:11:17PM +0900, Florian Gross wrote:
Edgardo Hames wrote:
>Hi everybody,
Moin.
>I tried to implement a program that
>compares two files for equality. I came up with the following trivial
>solution
>
>puts (IO.readlines file[0]) == (IO.readlines file[1])
>
>Have you got any other suggestions?
Not much different:
def files_equal?(*files)
files.map do |file|
File.size file
end.uniq.size <= 1 and
files.map do |file|
File.read file
end.uniq.size <= 1
end
This ought to be slightly faster in the average case.
Other optimizations would be reading the files line-wise in parallel and
bailing out as soon as one of the lines differs.
--
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. Anders Engström aengstrom@gnejs.net
. http://www.gnejs.net PGP-Key: ED010E7F
. [Your mind is like an umbrella. It doesn't work unless you open it.]
Florian Gross wrote:
>
> def files_equal?(*files)
> files.map do |file|
> File.size file
> end.uniq.size <= 1 and
> files.map do |file|
> File.read file
> end.uniq.size <= 1
> end
>
Could anybody please explain the "end.uniq.size" line?
Thanks!
Greetings,
Andreas
> This ought to be slightly faster in the average case.
>
> Other optimizations would be reading the files line-wise in parallel and
> bailing out as soon as one of the lines differs.
>