require 'diff/lcs/Array'

V

Venkat Kumar

Hi All,

I need compare two text files. I should require 'diff/lcs/Array' when i
do this iam getting error. Should i install any additional gem(if yes
pls share the link). I am using Scite editor. As iam very new to ruby
kindly help me in this.

Thanks,
Venkat
 
J

James Gray

I need compare two text files. I should require 'diff/lcs/Array'
when i
do this iam getting error.

I've tried using that library in the past, but its interface just
makes it crazy hard to work with. Now I would just do something like:

diff = `diff -u #{shell_escape path1} #{shell_escape path2} 2>&1`

I would define shell_escape() as something like:

def shell_escape(str)
String(str).gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\').
gsub(/\n/, "'\n'").
sub(/^$/, "''")
end


Hope that helps.

James Edward Gray II
 
D

Daniel Berger

I've tried using that library in the past, but its interface just =A0
makes it crazy hard to work with.

I came up with this for diff'ing 2 files and spitting the results to
stdout. It's meant to simulate "diff -u" output, but it's not quite
identical and is definitely not tested thoroughly:

# diff_test.rb
require 'diff/lcs'

# The file 'test1.txt' contains the following 3 lines:
#
# This is a house
# Hello World
# My favorite color is blue
#
# The file 'test2.txt' contains the following 3 lines:
#
# This is a car
# Hello World
# My favorite color is green

module Diff
module LCS
def self.diff_files(file1, file2)
array1 =3D IO.readlines(file1)
array2 =3D IO.readlines(file2)
diffs =3D diff(array1, array2)

print diffs.first[0].action * 3 + ' '
print file1
print "\t" + File.mtime(file1).to_s

puts

print diffs.first[1].action * 3 + ' '
print file2
print "\t" + File.mtime(file2).to_s

puts

diffs.each_with_index{ |diff, i|
diff.each{ |d|
puts d.action + d.element
}
}
end
end
end

Diff::LCS.diff_files('test1.txt', 'test2.txt')

Here was the output of Diff::LCS.diff_files:

--- test1.txt Wed Feb 25 12:18:42 -0700 2009
+++ test2.txt Wed Feb 25 12:18:53 -0700 2009
-This is a house
+This is a car
-My favorite color is blue
+My favorite color is green

Hope that was useful.

Regards,

Dan
 
V

Venkat Kumar

Hi ,

Thanks a lot for all ur replies.. My problem is I could't require
'diff/lcs'. I am getting an error message like.

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require': no such file to load -- diff/lcs/Array
(LoadError)

Should I install any additional gem. kindly help me

Thanks,
venkat

Daniel said:
I've tried using that library in the past, but its interface just �
makes it crazy hard to work with.

I came up with this for diff'ing 2 files and spitting the results to
stdout. It's meant to simulate "diff -u" output, but it's not quite
identical and is definitely not tested thoroughly:

# diff_test.rb
require 'diff/lcs'

# The file 'test1.txt' contains the following 3 lines:
#
# This is a house
# Hello World
# My favorite color is blue
#
# The file 'test2.txt' contains the following 3 lines:
#
# This is a car
# Hello World
# My favorite color is green

module Diff
module LCS
def self.diff_files(file1, file2)
array1 = IO.readlines(file1)
array2 = IO.readlines(file2)
diffs = diff(array1, array2)

print diffs.first[0].action * 3 + ' '
print file1
print "\t" + File.mtime(file1).to_s

puts

print diffs.first[1].action * 3 + ' '
print file2
print "\t" + File.mtime(file2).to_s

puts

diffs.each_with_index{ |diff, i|
diff.each{ |d|
puts d.action + d.element
}
}
end
end
end

Diff::LCS.diff_files('test1.txt', 'test2.txt')

Here was the output of Diff::LCS.diff_files:

--- test1.txt Wed Feb 25 12:18:42 -0700 2009
+++ test2.txt Wed Feb 25 12:18:53 -0700 2009
-This is a house
+This is a car
-My favorite color is blue
+My favorite color is green

Hope that was useful.

Regards,

Dan
 
D

Daniel Berger

Hi ,

Thanks a lot for all ur replies.. My problem is I could't require
'diff/lcs'. I am getting an error message like.

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require': no such file to load -- diff/lcs/Array
(LoadError)

Should I install any additional gem. kindly help me

Something may have gone wrong with your gem install. I would try
uninstalling diff-lcs and reinstalling it.

You haven't installed it manually also, have you? Maybe you have it in
your sitelib dir as well, and it's missing there.

Regards,

Dan
 
S

Suraj Kurapati

James said:
I would define shell_escape() as something like:

def shell_escape(str)
String(str).gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\').
gsub(/\n/, "'\n'").
sub(/^$/, "''")
end

Nice! I always wished this method was in the Ruby stdlib. Maybe it
could be added as a class method of the Shell class in the "shell"
stdlib?

require 'shell'
Shell.escape('foo "bar".txt')

Shall I file a feature request to ruby-core?
 
J

James Gray

James said:
I would define shell_escape() as something like:

def shell_escape(str)
String(str).gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\').
gsub(/\n/, "'\n'").
sub(/^$/, "''")
end

Nice! I always wished this method was in the Ruby stdlib. Maybe it
could be added as a class method of the Shell class in the "shell"
stdlib?

It's in Ruby 1.9. See my notes at the bottom of this post:

http://blog.grayproductions.net/articles/the_secret_shell_helper

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top