[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

require 'diff/lcs/Array'

Venkat Kumar

2/25/2009 5:18:00 PM

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
--
Posted via http://www.ruby-....

6 Answers

James Gray

2/25/2009 5:55:00 PM

0

On Feb 25, 2009, at 11:17 AM, Venkat Kumar wrote:

> 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


Daniel Berger

2/25/2009 7:44:00 PM

0



On Feb 25, 10:55=A0am, James Gray <ja...@grayproductions.net> wrote:
> On Feb 25, 2009, at 11:17 AM, Venkat Kumar wrote:
>
> > I need compare two text files. I should require 'diff/lcs/Array' =A0
> > when i
> > do this iam getting error.
>
> 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

Venkat Kumar

2/26/2009 5:05:00 AM

0

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 Berger wrote:
> On Feb 25, 10:55�am, James Gray <ja...@grayproductions.net> wrote:
>> On Feb 25, 2009, at 11:17 AM, Venkat Kumar wrote:
>>
>> > 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.
>
> 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

--
Posted via http://www.ruby-....

Daniel Berger

2/26/2009 3:00:00 PM

0



On Feb 25, 10:04=A0pm, Venkat Kumar <venkatesh...@gmail.com> wrote:
> 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

Suraj Kurapati

2/26/2009 10:06:00 PM

0

James Gray wrote:
> 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?
--
Posted via http://www.ruby-....

James Gray

2/26/2009 10:11:00 PM

0

On Feb 26, 2009, at 4:05 PM, Suraj Kurapati wrote:

> James Gray wrote:
>> 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_sh...

James Edward Gray II