# Require 'diff/lcs/Array'

**URL:** <https://www.ruby-forum.com/t/require-diff-lcs-array/161345>\
**Category:** Ruby\
**Created:** [February 25, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345 "2009-02-25T18:18:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![indium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@indium](https://www.ruby-forum.com/u/indium)\
**Post date:** [February 25, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345/1 "2009-02-25T18:18:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![indium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@indium](https://www.ruby-forum.com/u/indium)\
**Post date:** [February 25, 2009, 6:56pm UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345/2 "2009-02-25T18:56:49Z")

</div>

On Feb 25, 2009, at 11:17 AM, Venkat K. 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 G. II

---

<div class="post-metadata">

**Author:** ![indium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@indium](https://www.ruby-forum.com/u/indium)\
**Post date:** [February 26, 2009, 6:05am UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345/3 "2009-02-26T06:05:43Z")

</div>

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 B. wrote:

> On Feb 25, 10:55ï¿½am, James G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On Feb 25, 2009, at 11:17 AM, Venkat K. 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

---

<div class="post-metadata">

**Author:** ![indium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@indium](https://www.ruby-forum.com/u/indium)\
**Post date:** [February 26, 2009, 4:01pm UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345/4 "2009-02-26T16:01:26Z")

</div>

On Feb 25, 10:04 pm, Venkat K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![indium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@indium](https://www.ruby-forum.com/u/indium)\
**Post date:** [February 26, 2009, 11:06pm UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345/5 "2009-02-26T23:06:38Z")

</div>

James G. 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?

---

<div class="post-metadata">

**Author:** ![indium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@indium](https://www.ruby-forum.com/u/indium)\
**Post date:** [February 26, 2009, 11:12pm UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345/6 "2009-02-26T23:12:35Z")

</div>

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

> 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](http://blog.grayproductions.net/articles/the_secret_shell_helper)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![indium](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/35a633/32.png) [@indium](https://www.ruby-forum.com/u/indium)\
**Post date:** [February 25, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/require-diff-lcs-array/161345/7 "2009-02-25T20:45:43Z")

</div>

On Feb 25, 10:55 am, James G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Feb 25, 2009, at 11:17 AM, Venkat K. 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
